Java implementation Print file path and presentation file directory

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.