Skip to content

Instantly share code, notes, and snippets.

@SkyrocketStan
Last active July 9, 2021 09:43
Show Gist options
  • Save SkyrocketStan/7dd475fa96b2b43b8898a062766fecfc to your computer and use it in GitHub Desktop.
Save SkyrocketStan/7dd475fa96b2b43b8898a062766fecfc to your computer and use it in GitHub Desktop.
Get a List of all files and subfolders from a directory with Java
public List<String> getAllFilenamesAndDirectoriesSorted(String pathToDir) {
Path start = Paths.get(pathToDir);
try (Stream<Path> stream = Files.walk(start, Integer.MAX_VALUE)) {
return stream.map(String::valueOf).sorted().collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment