Requirements: traverse this tree structure
File (String pathname) ' \ \ ' In order to escape the ' \ '/// through the abstract path pathname create a new document or directory , "e:\\ Test\\aa "); // exists determine whether a file or directory exists; if (! parent.exists ()) {// mkdirs Create multilevel directory parent.mkdirs (); }
ImportJava.io.File; Public classTest { Public Static voidMain (String args[]) {File file=NewFile ("d:\\ #test");//This folder already exists in the computer, do not need to create a newlistall (file); //Traverse this folder (all files and folder names in the directory) } Private Static voidlistall (file file) {//gets all file objects and filenames under the specified directory .File[] F =File.listfiles (); for(File ff:f) {System.out.println (Ff.getabsolutepath ());
System.out.println (Ff.getname ()); } }}
ImportJava.io.File; Public classTest { Public Static voidMain (String args[]) {File file=NewFile ("d:\\ #test");//This folder already exists in the computer, do not need to create a newListall (file,0); //Traverse this folder (all files and folder names in the directory) } Private Static voidListall (File file,intLevel ) {System.out.println (Getsapce level)+file.getname ()); //gets all file objects and filenames under the specified directory .File[] F =File.listfiles (); level++; for(File ff:f) {if(Ff.isdirectory ()) {Listall (ff,level); }ElseSystem.out.println (Getsapce (level)+ff.getname ()); } } Public StaticString Getsapce (intLevel ) {StringBuilder SB=NewStringBuilder (); for(intx=0;x<level;x++) {sb.append ("|----"); } returnsb.tostring (); }}
Io Stream (File Object-practice-deep Traverse folder)
Java-io Stream (File object-depth traversal of folders and files under the specified directory)