Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void traverseDirectories(String path) {
- File directory = new File(path);
- if (directory.isDirectory()) {
- System.out.println(directory.getName() + ":");
- traverse(directory, 1);
- }
- }
- private static void traverse(File file, int indentLevel) {
- File[] files = file.listFiles();
- if (files != null) {
- for (File fileInArray : files) {
- String spacings = " ".repeat(indentLevel);
- if (fileInArray.isDirectory()) {
- System.out.println(spacings + fileInArray.getName() + ":");
- traverse(fileInArray, indentLevel + 1);
- } else {
- System.out.println(spacings + fileInArray.getName());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement