0

I have written a programme that receives a file as first argument and prints out a tree, but I want it to go deeper than just checking for subdirectories or file that the input file/folder contains.

import java.io.File;

public class DN12 {
    public static void main(String[] args)throws Exception {
        File file = new File(args[0]);
        String fPath = file.getPath();
        System.out.println(fPath);
        File[] paths = file.listFiles();
        for(int i=0; i<paths.length; i++) {
            String path2 = paths[i].getPath();
            String[] path3 = path2.split("/");
            System.out.println("  |___"+path3[path3.length-1]);
        }  
    }
}

Example input:

/p2/sources

My output:

/p2/viri
  |___sun.txt
  |___tree
  |___abc.txt

Expected output:

/p2/viri
  |___sun.txt
  |___tree
  |  |___.DS_Store
  |  |___dir1
  |  |  |___dir11
  |  |  |  |___b.txt
  |  |  |___a.txt
  |  |___src
  |  |  |___predavanja11
  |  |  |  |___TestDrzava.java
  |  |  |  |___Dnevi.java
  |  |  |  |___Drzave.java
  |  |  |  |___Oseba.java
  |  |  |  |___Delitelji.java
  |  |  |  |___Drzava.java
  |  |  |  |___Meseci.java
  |  |  |  |___TestOseba.java
  |  |___dir2
  |  |  |___dir21
  |  |  |___dir22
  |  |  |  |___e.txt
  |  |  |___d.txt
  |___abc.txt
  • 1
    Create method which you will be able to call recursively. Possibly related: [print directory tree](https://stackoverflow.com/q/10655085) – Pshemo May 27 '18 at 19:27

3 Answers3

1

You'll need to define a recursive method. In javaish pseudo code.

public static void listFiles(File f, int spaces) {
    String s = <BUILD STRING OF SPACES>;
    System.out.println(s + <YOUR_FILE_PRINT>);
    // print current file
    for (File f : f.listPaths()) {
        // Do printing.
        if (f.isDirectory()) {
            listFiles(f, spaces + 2); // recursive call
        }
    }
}

A recursive method needs to have a base case. In this case we only recurse if it's a directory and the stack always unwinds.

HSchmale
  • 1,838
  • 2
  • 21
  • 48
0

You can do it this way:

public static void main(String[] args) {
    displayDirRecursively("sources", 0);
}

public static void displayDirRecursively(String directory, int depth) {
    File file = new File(directory);
    if (depth > 0) {
        for (int i = 0; i < depth; i++) {
            System.out.print("   ");
        }
        System.out.print("|__");
    }
    System.out.println(file.getName());
    if (file.isDirectory()) {
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            displayDirRecursively(files[i].getPath(), depth + 1);
        }
    }
}
tkint
  • 321
  • 2
  • 5
0
public class Main {

  public static void main(String[] args) {
    new Main().fooCursive(new File(System.getProperty("user.dir")));

  }

  public void fooCursive(File file) {
    System.out.println(file.getName());
    if (file.exists() && file.isDirectory()) {
      Stream.of(file.listFiles()).forEach(subfile -> fooCursive(subfile));
    }
  }
}