Skip to content

deviknitkkr/TreePrinter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TreePrinter

To add this library to your project, click on JitPack badge. You can also manually add Jar file, you can find jar file in release section.

Demo: I have created two classes for printing FilesStructure.

1.FileTreePrinter

public class Main {

    public static void main(String[] args) {

        File file=new File("./src");

        FileTreePrinter treePrinter=new FileTreePrinter(file);
        treePrinter.visitAndPrint();
        //  or
        //  StringBuilder sb=treePrinter.visitAndReturn();
        //  sout(sb);
    }
}

This code will produce below output.

src
├─main
│  ├─java
│  │  └─com
│  │     ├─demo
│  │     │  └─Main.java
│  │     └─devik
│  │        ├─Color.java
│  │        ├─file
│  │        │  ├─FileTreePrinter.java
│  │        │  └─FileTreePrinterColored.java
│  │        └─TreePrinter.java
│  └─resources
└─test
   └─java

2.FileTreePrinterColored

public class Main {

    public static void main(String[] args) {

        File file=new File("./src");

        FileTreePrinterColored treePrinter=new FileTreePrinterColored(file);
        treePrinter.visitAndPrint();
        //  or
        //  StringBuilder sb=treePrinter.visitAndReturn();
        //  System.out.println(sb);
    }
}

This code will produce below output.

image

Also in case of FileTreePrinterColored you can set custom colors.

public class Main {

    public static void main(String[] args) {

        File file=new File("./src");

        FileTreePrinterColored treePrinter=new FileTreePrinterColored(file);
        
        treePrinter.setFolderColor(Color.RED);
        treePrinter.setHiddenFolderColor(Color);
        treePrinter.setExecutableFileColor(Color);
        treePrinter.setNonExecutableFileColor(Color);
        //or
        treePrinter.setNonExecutableFileColor(Color.valueOf("color asci"));

        
        treePrinter.visitAndPrint();
        //  or
        //  StringBuilder sb=treePrinter.visitAndReturn();
        //  System.out.println(sb);
    }
}

If you want to print other than file tree than you can use TreePrinter abstract class. In that case you need to implement three methods.

 Object obj;
        TreePrinter treePrinter=new TreePrinter(obj) {
            @Override
            public Object[] getChild(Object obj) {
                return new Object[0];
            }

            @Override
            public String getValue(Object obj) {
                return null;
            }

            @Override
            public boolean isLeaf(Object obj) {
                return false;
            }
        };
        // Here obj is the root of TreeStructure.

for ie. if you want to print BinaryTree than you can do something like below...

Object obj; //here obj is root of Binary tree
       TreePrinter treePrinter=new TreePrinter(obj) {
           @Override
           public Object[] getChild(Object obj) {
               Node node=(Node)obj;
               return new Object[]{node.left,node.right};
           }

           @Override
           public String getValue(Object obj) {
               return ((Node)obj).getValue();
           }

           @Override
           public boolean isLeaf(Object obj) {
              Node node=(Node)obj;
               return node.left==null && node.right==null;
           }
       };
       // Here obj is the root of TreeStructure.