The following code is introduced to the method of the specified directory and all files under the tree structure, including the files in the subdirectory, and the effective drawing is attached.
Import Java.io.file; Public Class Readdirectory {// The number of layers where the file is located, the file name or directory name of the output format of the output format* @param name* @param level Record The level of the name of the name* @Return output*/ Public String CreatePrintstr (String name, int level) {// output prefix string printstr = "" ""; // The indentation of for (int i =; i < Level; I ++) {Printstr = Printstr + "" ";} PrintStr = Printstr +"- " + Name; Return Printstr;} / *** The directory of the initial given* @Param Dirpath* / Public Void Printdir (String Dirpath) {// Divide the given directory to divide string [] diRNAMELIST = DIRPATH.SPLIT ("/////"); // Set the base field.length of the file level = diRNAMELINGTH; // Format output for (int i =; I <diRNamelist.length; i ++) {system.out.println (createPrintstr (diNamelist [i], i));} /*** output files in the given directory, Including files in the subdirectory* @Param Dirpath given a directory*/ Public Void ReadFile (String Dirpath) {// Create the file object of the file in the current directory File File File = New File (DIRPATH); File object array of files file [] list = file.listfiles (); // traverses the file array for (int i =; i <list.length; i ++) {if (list.isdirect ())) {System.out.printlnln (Createprintstr (list.getName (), filevel)); FileLelevel ++; // Recalling 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: // There are:"; PATH); RD. Readfile (Dirpath);}} The output result is as follows:
The above is all the contents of this article. I hope everyone likes it.