Today, no matter, I realized a print file and display the file directory of small functions; Enter a file or directory, if the file to print its path and name, if it is a directory, the optional file suffix, if not selected, print all the files under it, if you choose the suffix name, print all the files under the suffix name. The code is as follows:
/** Enter a file or directory, if the file to print its path and name, * if it is a directory, optional file suffix, if not selected, print all the files under it, * if you choose the suffix name, print all the files under the suffix name * * * Mist lee,2014-11-29*/ImportJava.io.File;ImportJava.io.FilenameFilter;Importjava.io.IOException;ImportJava.util.Scanner; Public classDisplayfilestate {Private Static BooleanFlag =false; Public Static voidMain (string[] args)throwsIOException {//String fileName = Rawinput ();Scanner in =NewScanner (system.in); System.out.print ("Please enter the file or directory path:"); String FileName=In.nextline (); File F=NewFile (fileName); if(F.isfile ()) {System.out.println ("The path entered is a file"); System.out.println (F.getcanonicalpath ()); } Else if(F.isdirectory ()) {System.out.println ("The path entered is a directory"); System.out.println ("Please enter the suffix name of the file you want to find, and if it is blank, show All Files"); String ext=In.nextline (); System.out.println ("The suffix name of the file you are looking for is:" +ext); Spreadfiledir (F,ext); } Else{System.out.println ("Input error, please confirm and re-enter!" "); } if(flag) System.out.println ("I can't find the documents you need."); } //Expand Catalog File Private Static voidSpreadfiledir (File F, String ext)throwsIOException {string[] fileNames=f.list (); //Boolean flag = false; for(inti=0;i<filenames.length;i++) {File subfile=NewFile (F.getpath (), filenames[i]); if(Subfile.isfile ()) {if(Ext.equals ("") ) System.out.println (Subfile.getcanonicalpath ()); Else{extensionfilter Extfilter=Newextensionfilter (EXT); BooleanIsTrue =extfilter.accept (F, filenames[i]); if(isTrue) System.out.println (Subfile.getcanonicalpath ()); ElseFlag=true; //flag = Istrue^false; } } Else if(Subfile.isdirectory ()) {Spreadfiledir (subfile,ext); } Else{System.out.println ("Something is worong!"); } } }}//secondary filter class to handle file suffix namesclassExtensionfilterImplementsfilenamefilter{ PublicExtensionfilter (String ext) {extension= "." +ext; } @Override Public BooleanAccept (File dir, String name) {//TODO auto-generated Method Stub returnname.endswith (extension); } PrivateString extension; }
Java implementation Print file path and presentation file directory