Depth stream stream in Java 8

I would like to traverse a directory structure in Java 8 using the Stream API in depth. The reason for this is that I want to sort the content in the files according to the timestamp present in each file, for each directory. Basically, I'm looking for something similar to # walk Files , but for directories. How can I achieve this?

+3


source to share


2 answers


It is difficult to understand your description of the problem, in the first place what you actually want to do when you say you want to "sort the content in the files according to the timestamp present in each file, based on each directory."

And when you say you want "something like File Passes, but for directories," that means you have a use problem Files.walk

with directories, but it's not clear what problem you have.

Ie, you can just list all subdirectories of the current directory like



Files.walk(Paths.get("")).filter(Files::isDirectory).forEach(System.out::println);

      

So, if that doesn't fit your goal, you should spend more time developing your goal.

+2


source


Using StreamEx is a breeze:



File root = new File("someFilePath");
StreamEx.ofTree(root, x -> StreamEx.of(x.listFiles(File::isDirectory)))
    .map(File::getAbsolutePath) // or whathever you need to do with the folder
    .forEach(System.out::println); // the same as previous line

      

+4


source







All Articles