Java gets the specified file under the specified path/java.io.file.listfiles (filenamefilter filter)

Source: Internet
Author: User

Java.io.File.listFiles (filenamefilter filter) returns the abstract path an array group, which represents the files and directories in the directory that are represented by this abstract pathname, which satisfies the specified filter.

Statement

The following is a declaration of the Java.io.File.listFiles (FilenameFilter filter) Method:

PublicFile[] listfiles(filenamefilter filter)      
Parameters
    • Filter-File name filter

return value

The method returns the abstract path of the An array group, which represents the file and directory in the directory where this abstract pathname is represented, which satisfies the specified filter.

Abnormal
    • SecurityException--If the security manager exists and its securitymanager.checkread (java.lang.String) method denies read access to the file

Example

The following example shows the use of the Java.io.File.listFiles (FilenameFilter filter) method.

PackageCom.Yiibai;ImportJava.Io.File;ImportJava.Io.FilenameFilter;Public Class Filedemo { Public Static voidMain(String[]Args) { FileF= Null; File[]Paths; Try{ Create new fileF= New File("C:/test"); Create new filename Filter FilenameFilterFilenameFilter= New FilenameFilter() { @Override Public BooleanAccept(FileDir, StringName) { If(Name.LastIndexOf(‘.‘) >0) { Get last index for '. ' Char IntLastIndex=Name.LastIndexOf(‘.‘); Get extension StringStr=Name.Substring(LastIndex); Match path name extension If(Str.equals(". txt")) { Return True; } } Return False; } }; Returns pathnames for files and directoryPaths=F.Listfiles(FilenameFilter); For each pathname in pathname array For(FilePath:Paths) { //prints file and directory Paths system.out.println (path); }}catch (Exception e) {//If any error occurs e.printstacktrace ();}}}        

Let's compile and run the above program, which will produce the following results:

C:estchild_test.  TXT   

Java gets the specified file under the specified path/java.io.file.listfiles (filenamefilter filter)

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.