Java Recursive Traversal directory
First, the content
Output absolute path to all files (including directories) under the specified directory (folder)
Second, source code: Recursivelistdirectory.java
1 Packagecn.com.zfc.day016;2 3 ImportJava.io.File;4 5 /**6 * @describe recursively traverse the directory7 * @authorZFC8 * @date January 1, 2018 morning 8:44:559 */Ten Public classRecursivelistdirectory { One A Public Static voidMain (string[] args) { -String directoryname = "F:\\studysoft"; - //1. Mapping Directory Files theFile directory =NewFile (directoryname); - //2. Call Method - recursivelistdirectory (directory); - } + - /** + * Output The name of the file under the specified directory, including sub-directories A * at * @paramdirectoryname: Directory name - * @authorZFC - * @date January 1, 2018 morning 8:46:08 - * - */ - Public Static voidrecursivelistdirectory (File directory) { in //1. Does the mapping directory file exist? - if(!directory.exists ()) { to //does not exist then returns directly + return; - } the //2. Is it a catalogue? * if(!directory.isdirectory ()) { $ //not a directory, to determine whether it is a file? Panax Notoginseng if(Directory.isfile ()) { -System.out.println ("File absolute path:" +Directory.getabsolutepath ()); the } +}Else { A //is the directory that gets all the files (including directories) under the directory thefile[] Files =directory.listfiles (); + //determine if files are empty? - if(NULL!=files) { $ //iterating through an array of files $ for(File f:files) { - //determine if it is a directory? - if(F.isdirectory ()) { the //is the directory -System.out.println ("Directory absolute path:" +F.getabsolutepath ());Wuyi recursivelistdirectory (f); the}Else { - //not a directory, to determine whether it is a file? Wu if(F.isfile ()) { -System.out.println ("File absolute path:" +F.getabsolutepath ()); About } $ } - } - } - } A } +}
Iii. Results of operation
Java Recursive Traversal directory