JAVA File class Print directory tree structure graph recursive algorithm

Source: Internet
Author: User

To achieve the purpose of putting all the files under the development directory, print them out in a tree-like structure. The code is as follows:

Package Cn.bjsxt.io;

Import Java.io.File;

public class Filetree {
public static void Main (string[] args) {
File F=new file ("e:/useful Document"); Suppose you print all the files in this directory
Printtree (f, 0); Starting from itself, from 0

}

public static void Printtree (File f,int level) {
for (int i=0;i<=level-1;i++) {
System.out.print ("-"); Not at first--one more "-" in front of the first-level catalogue
}

System.out.println (F.getname ());
if (F.isdirectory ()) {
File[] Files=f.listfiles ();

for (File j:files) {
Printtree (j,level+1); To enhance the For loop, J is the parameter. Each end is then used Printtree (),
Level levels are added one at a time.
}
}

}
}

JAVA File class Print directory tree structure graph recursive algorithm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.