0

I am trying really hard to make my code to print the files and the directories in the same way that the command 'tree' in unix print.

What do I need to do so it will work? I'm stuck on it to much hours :(

This is the class I created for simple implementation of tree command using the composite design pattern:

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class TreePrint {
    private final Directory root;

    public TreePrint(String path) {
        root = new Directory(path);
    }

    public void printTree(){
        root.print(0);
    }

    private interface FileSystemComponent {
        void print(int level);
    }

    private static class FileComponent implements FileSystemComponent{
        private final String fileName;

        public FileComponent(String name) {
            fileName = name;
        }

        @Override
        public void print(int level) {
            printLevel(level);
            System.out.println(fileName);
        }
    }

    private static class Directory implements FileSystemComponent{
        private final String directoryName;
        private final List<FileSystemComponent> fileList = new ArrayList<>();

        public Directory(String path) {
            File directory = new File(path);
            if(!directory.exists()){
                throw new IllegalArgumentException("Invalid path, please try again");
            }

            directoryName = directory.getName();
            File[] components = directory.listFiles();

            if(null != components) {
                for (File file : components) {
                    if (file.isDirectory()) {
                        fileList.add(new Directory(file.getPath()));
                    } else {
                        fileList.add(new FileComponent(file.getName()));
                    }
                }
            }
        }

        @Override
        public void print(int level) {
            printLevel(level);
            System.out.println(colorTextToBlue(directoryName));
            for(FileSystemComponent file : fileList) {
                file.print(level + 1);
            }
        }
    }

    private static void printLevel(int level){
        System.out.print("├──");
        for (int i = 0; i < level; ++i) {
            System.out.print("──");
        }
    }

    private static String colorTextToBlue(String text){
        final String blueBold = "\033[1;34m";
        final String reset = "\033[0m";

        return blueBold + text + reset;
    }
}

For example, I want to print like this:

├── makefile
├── quiz19.c
├── stack_min.c
└── stack_min.h

but it's print like this:

├──quiz19
├────makefile
├────quiz19.c
├────stack_min.c
├────stack_min.h

(I still wan't the root on top, but i just want the lines to be the same like int the last file)

3
  • Please be more specific than “What do I need to do so it will work?” What about the program’s behavior is different from what you were expecting? Commented Aug 17, 2022 at 18:32
  • add the comparison Commented Aug 21, 2022 at 9:04
  • I gather you want the last line to print └── rather than ├──? Commented Aug 21, 2022 at 19:03

0

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.