How do I recursively list all files under a directory in Java? Does the framework provide any utility?
I saw a lot of hacky implementations. But none from the framework or nio
How do I recursively list all files under a directory in Java? Does the framework provide any utility?
I saw a lot of hacky implementations. But none from the framework or nio
No external libraries needed.
Returns a Collection so you can do whatever you want with it after the call.
This code are ready to run
Non-recursive BFS with a single list (particular example is searching for *.eml files):
just write it yourself using simple recursion:
FileUtils have
iterateFiles
andlistFiles
methods. Give them a try. (from commons-io)Edit: You can check here for a benchmark of different approaches. It seems that the commons-io approach is slow, so pick some of the faster ones from here (if it matters)
Apart from the recursive traversal one can use a Visitor based approach as well.
Below code is uses Visitor based approach for the traversal.It is expected that the input to the program is the root directory to traverse.