print directory tree

前端 未结 10 1480
傲寒
傲寒 2020-12-03 08:45

I have to print a directory tree (like tree command), example:

 .
 +---A
 |   +---IMAGES
 |       +---BACKUP
 +---ADOKS
 |   +---ROZDZIAL_2
 |   +---ROZDZIAL         


        
10条回答
  •  长情又很酷
    2020-12-03 09:13

    import java.io.File;
    
    public class MainEntry {
    public static void main(String[] args) {
        walkin(new File("/home/user")); //Replace this with a suitable directory
    }
    
    /**
     * Recursive function to descend into the directory tree and find all the files 
      @param dir A file object defining the top directory
     **/
    public static void walkin(File dir) {
    
        File listFile[] = dir.listFiles();
        if (listFile != null) {
            for (int i=0; i

    }

    Will work fine..The Logic is correct

提交回复
热议问题