<File.walk()> includes the provided file - is ther...
# getting-started
a
File.walk() includes the provided file - is there a nice way to not do that, and only list the contents? I want an equivalent to the Java stdlib
File.listFiles()
, which isn't nice because it returns a nullable array.
Copy code
└── some-dir/
    ├── a.txt
    ├── b.json
    ├── c.xml
    └── sub-contents/
        └── x.txt
I want to do
Copy code
File("$basePath/some-dir").walk().maxDepth(1)
and get the files
a.txt
,
b.json
,
c.xml
. And not
some-dir
, or
sub-contents
s
Oleg's functions look very nice but are still experimental. But you could create your own extension function for now:
Copy code
fun File.walkFiles(maxDepth: Int): List<File> =
    this.walk().maxDepth(maxDepth).filter{it.isFile()}.toList()
if you don't need the number of Files is more efficient to return a Sequence of Files instead
Copy code
fun File.walkFiles(maxDepth: Int): Sequence<File> =
    this.walk().maxDepth(maxDepth).filter{it.isFile()}