Advertisement
Georgi_Benchev

Untitled

Dec 6th, 2024
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.83 KB | None | 0 0
  1. public static void traverseDirectories(String path) {
  2.         File directory = new File(path);
  3.  
  4.         if (directory.isDirectory()) {
  5.             System.out.println(directory.getName() + ":");
  6.             traverse(directory, 1);
  7.         }
  8.     }
  9.  
  10.     private static void traverse(File file, int indentLevel) {
  11.  
  12.         File[] files = file.listFiles();
  13.         if (files != null) {
  14.             for (File fileInArray : files) {
  15.                 String spacings = "  ".repeat(indentLevel);
  16.                 if (fileInArray.isDirectory()) {
  17.                     System.out.println(spacings + fileInArray.getName() + ":");
  18.                     traverse(fileInArray, indentLevel + 1);
  19.                 } else {
  20.                     System.out.println(spacings + fileInArray.getName());
  21.                 }
  22.             }
  23.         }
  24.     }
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement