Kotlin – Iterate through all files in a directory – Examples

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.

Kotlin Examples – Traverse or iterate through all files in a directory using Kotlin

walk() – using java.io.File.walk()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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)

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

/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

walkBottomUp() – using java.io.File.walkBottomUp()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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)

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

/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

walkTopDown() – using java.io.File.walkTopDown()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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)

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

/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() or java.io.File.walkBottomUp() functions traverse or iterate through all files in a directory with the help of Kotlin Examples.