Display tree format directories

Asked

Viewed 91 times

2

I have this code here in JAVA where I need lists of folders and subfolders in tree format, but I can’t get it. Any help?

package Control;

import java.awt.Desktop;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class PercorreDir {


    public static StringBuffer buffer = new StringBuffer();

    public static int nespaço = 1;
    public static String espaço = "";

    public static StringBuffer percorre(File caminho){

        if(caminho.isDirectory()){

            buffer.append(espaço + caminho.getName()+"\n");

            for (File subpasta: caminho.listFiles()) {          

                for (int cta = 0; cta < nespaço; cta++){
                    espaço += "    ";
                }

                nespaço += 1;

                percorre(subpasta);
                }
        } else {
            nespaço = 0;
            espaço = "    ";

        }

        return buffer;

    }
}
  • Your problem is in space, right? By the code above they must be "out of control"... (if it is something else, please clarify, I am writing an answer addressing this)

  • Yes, it’s the spaces. I’ll edit and put a picture of how the output comes out

  • No need, I can imagine... :)

1 answer

2


If the problem is that whenever you find a file you "reset" the number of spaces to zero, which causes the next folder in the queue to be printed as if it were on the root. It is not necessary to do anything special in the case of files (since you do not want to treat them), instead I suggest increasing the number of spaces before going through the subfolder, and at the end decreasing again. Or better yet, don’t use a global variable (static), but pass the prefix of each folder as parameter:

public static StringBuffer percorre(File caminho){
    return percorre(caminho, "");
}

public static StringBuffer percorre(File caminho, String prefixo){

    if(caminho.isDirectory()){

        buffer.append(prefixo + caminho.getName()+"\n");

        String novoPrefixo = prefixo + "    ";

        for (File subpasta: caminho.listFiles()) {          
            percorre(subpasta, novoPrefixo);
        }
    }

    return buffer;

}
  • Note: This answer assumes that the root folders start with no space at the beginning. If you want them to have 4 spaces before, just change the initial condition to return percorre(caminho, " ");.

  • Thank you very much! But what if I wanted to list the files that are in the subfolder? How would I do?

  • Well, you can put one else { buffer.append(prefixo + caminho.getName()+"\n") }, or simply perform this operation before the if (since it is the same for both folders and files). But if you want to differentiate them in some way - for example putting [P] or [A] in front before printing it - then it is better to do in else even.

  • Thanks man! Saved my semester haha

Browser other questions tagged

You are not signed in. Login or sign up in order to post.