David Pesetsky
David Pesetsky

Reputation: 294

Find a file in Kotlin

I need to find a file in a directory using Kotlin. Something like:

ls *.bld

If there are a couple results, that's ok. It would be a short list. Now there's thousands of files.

Upvotes: 3

Views: 4638

Answers (2)

Reijo Korhonen
Reijo Korhonen

Reputation: 436

With kotlin there in build in listDirectoryEntries

Upvotes: 1

Animesh Sahu
Animesh Sahu

Reputation: 8106

You could create a File object for a directory and then filter on the walk.

val path = File("/path/to/files/directory")
path.walk().filter { it.name.endsWith(".bld") }

PS: walk() returns a Sequence of contents of directory

Upvotes: 8

Related Questions