Kotlin – Iterate through All Files in a Directory

We shall use the extension functions  of java.io.File : walk(), walkBottomUp, walkTopDown()  to generate kotlin.io.FileTreeWalk on which we use iterator to traverse the directory and its contents. The functionality of the three functions is almost same except the order in which they iterate through contents.

In the following examples, we will take a folder tree as shown below, to list out the files iteratively in a directory.

Traverse or iterate through all files in a directory using Kotlin - Kotlin Example - Kotlin Tutorial - www.tutorialkart.com


Iterate through All Files using java.io.File.walk()

In this example, we will use File.walk() method to traverse the files in the directory and its sub-directories recursively.

example.kt

</>
Copy
import java.io.File

/**
 * Kotlin Example to traverse directory and its contents
 */
fun main(args: Array) {

    // using extension function walk
    File("/home/arjun/tutorials/").walk().forEach {
        println(it)
    }
}

Output

/home/tutorialkart/tutorials
/home/tutorialkart/tutorials/info.txt
/home/tutorialkart/tutorials/list.txt
/home/tutorialkart/tutorials/android
/home/tutorialkart/tutorials/android/textview.txt
/home/tutorialkart/tutorials/android/button.txt
/home/tutorialkart/tutorials/kotlin
/home/tutorialkart/tutorials/kotlin/constructor.txt
/home/tutorialkart/tutorials/kotlin/expressions.txt
/home/tutorialkart/tutorials/java
/home/tutorialkart/tutorials/java/polymorphism.txt

Iterate through All Files using java.io.File.walkBottomUp()

In this example, we will use File.walkBottomUp() method to list out the files in a given directory and its sub-directories.

example.kt

</>
Copy
import java.io.File

/**
 * Kotlin Example to traverse directory and its contents
 */
fun main(args: Array) {

    // using extension function walkBottomUp
    File("/home/arjun/tutorials/").walkBottomUp().forEach {
        println(it)
    }
}

Output

/home/tutorialkart/tutorials/info.txt
/home/tutorialkart/tutorials/list.txt
/home/tutorialkart/tutorials/android/textview.txt
/home/tutorialkart/tutorials/android/button.txt
/home/tutorialkart/tutorials/android
/home/tutorialkart/tutorials/kotlin/constructor.txt
/home/tutorialkart/tutorials/kotlin/expressions.txt
/home/tutorialkart/tutorials/kotlin
/home/tutorialkart/tutorials/java/polymorphism.txt
/home/tutorialkart/tutorials/java
/home/tutorialkart/tutorials

Iterate through All Files using java.io.File.walkTopDown()

In this example, we will use File.walkTopDown() method to list out the files in a given directory and its sub-directories.

example.kt

</>
Copy
import java.io.File

/**
 * Kotlin Example to traverse directory and its contents
 */
fun main(args: Array) {

    // using extension function walkTopDown
    File("/home/arjun/tutorials/").walkTopDown().forEach {
        println(it)
    }
}

Output

/home/tutorialkart/tutorials
/home/tutorialkart/tutorials/info.txt
/home/tutorialkart/tutorials/list.txt
/home/tutorialkart/tutorials/android
/home/tutorialkart/tutorials/android/textview.txt
/home/tutorialkart/tutorials/android/button.txt
/home/tutorialkart/tutorials/kotlin
/home/tutorialkart/tutorials/kotlin/constructor.txt
/home/tutorialkart/tutorials/kotlin/expressions.txt
/home/tutorialkart/tutorials/java
/home/tutorialkart/tutorials/java/polymorphism.txt

Conclusion

In this Kotlin Tutorial, we have learnt to use java.io.File.walk(), java.io.File.walkTopDown() and java.io.File.walkBottomUp() functions to traverse or iterate through all files in a directory and its sub-directories recursively, with the help of Kotlin Examples.