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.