Java traversal output Specify directory, tree structure All files include files under subdirectories _java

Source: Internet
Author: User
Tags readfile

The following section of code describes the Java output specified directory, tree structure, all files under the file including subdirectories of the method, with the effect of the diagram.


Import Java.io.File;
  public class Readdirectory {//File number of layers private int filelevel; /** * Generate output format * @param name output file name or directory name * @param level output file name or hierarchy of directory name * @return Output string/public string Create
   Printstr (string name, int level) {//output prefix String printstr = "";
   Indent by hierarchy for (int i =; I < level; I + +) {printstr = Printstr + "";
   } printstr = Printstr + "-" + name;
  return printstr; /** * Output the initial given directory * @param dirpath the given directory/public void Printdir (String dirpath) {//split the given directory string[] D
   Irnamelist = Dirpath.split ("\\\\");
   Set file level base filelevel = Dirnamelist.length;
   Output for (int i =; I < dirnamelist.length i + +) {System.out.println (Createprintstr (dirnamelist[i), i)); }/** * Outputs files in a given directory, including files in subdirectories * @param dirpath a given directory/public void ReadFile (String dirpath) {//Create the current directory
   The file object, filename = new file (Dirpath); Gets an array of file objects representing all files in the directory file[] list = File.listfileS (); Traversal of the file array for (int i =; I < list.length i++) {if (List.isdirectory ()) {System.out.println (createprint
     Str (List.getname (), filelevel));
     Filelevel + +;
     Recursive subdirectory ReadFile (List.getpath ());
    Filelevel--;
    else {System.out.println (Createprintstr (List.getname (), filelevel));
   }} public static void Main (string[] args) {readdirectory rd = new readdirectory ();
   String Dirpath = "d:\\ Youdao dictionary";
   Rd.printdir (Dirpath);
  Rd.readfile (Dirpath);
 }
 }

The output results are as follows:

The above is the entire content of this article, I hope you like.

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.