Skip to content

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

publicclassMain {
publicstaticvoidmain(String[] args) {
Filefile=newFile("./src");
FileTreePrintertreePrinter=newFileTreePrinter(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

publicclassMain {
publicstaticvoidmain(String[] args) {
Filefile=newFile("./src");
FileTreePrinterColoredtreePrinter=newFileTreePrinterColored(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.

publicclassMain {
publicstaticvoidmain(String[] args) {
Filefile=newFile("./src");
FileTreePrinterColoredtreePrinter=newFileTreePrinterColored(file);
treePrinter.setFolderColor(Color.RED);
treePrinter.setHiddenFolderColor(Color);
treePrinter.setExecutableFileColor(Color);
treePrinter.setNonExecutableFileColor(Color);
//ortreePrinter.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.

Objectobj;
TreePrintertreePrinter=newTreePrinter(obj) {
@OverridepublicObject[] getChild(Objectobj) {
returnnewObject[0];
}
@OverridepublicStringgetValue(Objectobj) {
returnnull;
}
@OverridepublicbooleanisLeaf(Objectobj) {
returnfalse;
}
};
// Here obj is the root of TreeStructure.

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

Objectobj; //here obj is root of Binary treeTreePrintertreePrinter=newTreePrinter(obj) {
@OverridepublicObject[] getChild(Objectobj) {
Nodenode=(Node)obj;
returnnewObject[]{node.left,node.right};
}
@OverridepublicStringgetValue(Objectobj) {
return ((Node)obj).getValue();
}
@OverridepublicbooleanisLeaf(Objectobj) {
Nodenode=(Node)obj;
returnnode.left==null && node.right==null;
}
};
// Here obj is the root of TreeStructure.

About

This code can be used for printing the tree structure.

Topics

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages