Filters for Java files

Source: Internet
Author: User

The method of traversing files in a file object is basically the difference between the return values, a list () method that returns a string of strings, and a listfile () method that returns an array of the file objects. Or two methods with parameters, you can traverse a file of a specified type, such as a. mp3 file .... It is useful to filter the child files or sub-paths.

Public string[] List ()

Public string[] List (filenamefilter filter)
The filenamefilter here is public interface FilenameFilter the class instance implementing this interface is available for the filter file name. This implements the Accept (File dir, String name)
The method in which Dir-the directory where the found file resides. This is the path to the file object.
Name-Names of the files. The name of all files under the file object path.
Two parameters. This method iterates over the specified file subdirectory subfolder. If the method returns True, the list lists the subdirectory or subfolder.

 Public classFilefiletertest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubFile file=NewFile ("E://java's Jdk//lib"); String[] FileName=file.list (NewFileter ());  for(String string:filename) {System.out.println (string+"  "); }    }}  /*** Implement Filenamefileter interface, implement the method of filtering files * If the method returns True, list lists the sub-file or subdirectory. * @authorCang **/   classFileterImplementsFilenameFilter {@Override//The name is the names of all the files or paths in file, the first level, and Dir is the absolute path to the document object.      Public BooleanAccept (File dir, String name) {//TODO auto-generated Method Stub        returnName.endswith (". Jar") | |NewFile (dir+ "//" +name). Isdirectory (); }   }

Filters for Java files

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.