如何在kotlin中使用File.walk

时间:2018-06-04 14:42:33

标签: kotlin

我正试图以这种方式使用file.walk走进文件夹:

File.walk(FileWalkDirection.BOTTOM_UP).forEach()

文档说:

  

在kotlin.io.FileWalkDirection中定义的枚举条目BOTTOM_UP

     

深度优先搜索,目录在其文件后访问

     

枚举常数序数:1

如果我使用FileWalkDirection.BOTTOM_UP,那就是我的打印步行

emergency-support/digital/beginner/.category.yml
emergency-support/digital/.category.yml
emergency-support/physical/beginner/.category.yml
emergency-support/physical/.category.yml
emergency-support/.category.yml

我想走这个结果:

emergency-support/.category.yml
emergency-support/physical/.category.yml
emergency-support/physical/beginner/.category.yml
emergency-support/digital/.category.yml
emergency-support/digital/beginner/.category.yml

如何在目录之前遍历访问文件?

1 个答案:

答案 0 :(得分:3)

I have to admit that I did not totally undersand your question. If all you want is to print the files first and than the directories and you don't have any other concern you can use the 'sortedBy' function:

File
    .walk(FileWalkDirection.BOTTOM_UP)
    .sortedBy { it.isDirectory }
    .forEach { println(it) }