0

I need to return or print a file/directory tree using stack data structure. For example:

Folder1
    Folder1.1
        File1.1.1
    Folder1.2
Folder2
    File2.1
    File2.2
...

My code so far:

public static void filetree(File mainfolder, int indent){

    Stack<String> filesanddirectories = new Stack<>();

        for (File file : mainfolder.listFiles()){
            if (file.isDirectory()){
                filesanddirectories.push(file.getName());
                filetree(file, 0);
            }
            else if (file.isFile()){
                filesanddirectories.push(file.getName());
            }
        }

    for (int i = 0; i < filesanddirectories.size(); i++){
        System.out.println(filesanddirectories.pop());
    }
}

This code prints folders and files but with no indention, backwards and not exactly in the right order.

Could someone explain the logic of how it should work?

EDIT: Found a solution using recursion and stack (although stack seems to be unnecessary)

6
  • To get the indenting working you need to add something to the indent value every time you recurse - something like filetree(file, 0); -> filetree(file, indent + 4);. You will then need to change your printing mechanism to use the indent value. Commented Oct 16, 2018 at 10:43
  • To get the file names printing correctly you need to print the file names when you see them. if (file.isFile()){filesanddirectories.push(file.getName()); -> if (file.isFile()){System.out.println(.... Commented Oct 16, 2018 at 10:44
  • 1
    So print files and folders and add folders to stack? Commented Oct 16, 2018 at 10:51
  • you should print it by recursive method Commented Oct 16, 2018 at 12:28
  • 1
    Using a stack for this when you have a recursive method is somewhat unusual. Are you sure you must use a stack? Commented Oct 16, 2018 at 16:32

1 Answer 1

0

I used indent and temp variables to keep the indent when traversing recursively through the file directory. Then printed indent at the start of the for loop.

public static void filetree(File mainfolder, int indent) {

        int temp;

        for (File file : mainfolder.listFiles()) {
            for(int i = 0; i<indent; i++) {
                System.out.print("  ");
            }
            temp = indent;
            if (file.isDirectory()) {

                indent++;
                System.out.println(file.getName()); 

                filetree(file, indent);
                indent--;

            } else if (file.isFile()) {

                System.out.println(file.getName());
                indent = temp;
            } 
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

What is the point of pushing things onto a stack if you're never going to pop them?
filesanddirectories.push(file.getName()); was in OP's code, I just didn't delete the lines. Using stack has no contribution to the solution. Maybe i should update my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.