Created
March 25, 2017 08:33
-
-
Save SnuktheGreat/c98e7e3827265d249966f2de85a09a5a to your computer and use it in GitHub Desktop.
List files in a Stream recursively (depth-first)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.util.Arrays; | |
import java.util.stream.Stream; | |
public class StreamFiles { | |
public static Stream<File> recursive(File file) { | |
File[] listed = file.listFiles(); | |
if (listed == null) { | |
return Stream.of(file); | |
} | |
return Stream.concat( | |
Stream.of(file), | |
Arrays.stream(listed).flatMap(StreamFiles::recursive)); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment