Skip to content

Instantly share code, notes, and snippets.

@SnuktheGreat
Created March 25, 2017 08:33
Show Gist options
  • Save SnuktheGreat/c98e7e3827265d249966f2de85a09a5a to your computer and use it in GitHub Desktop.
Save SnuktheGreat/c98e7e3827265d249966f2de85a09a5a to your computer and use it in GitHub Desktop.
List files in a Stream recursively (depth-first)
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