How to obtain a list of files that meet the conditions in Java

Source: Internet
Author: User

Java uses the file. listfiles/list method to list objects in a directory. The following describes the usage of the file. listfiles method. The usage of file. List is also basically the same.
The file. listfiles method has three forms,
Public file [] listfiles ()
Listfiles without parameters will return all files, including sub-files and sub-directories.
Public file [] listfiles (Java. Io. filenamefilter)
Java. Io. filenamefilter: file name filter interface. The filter must implement this interface. This interface defines
Public Boolean accept (File file, string filename) method. The first parameter file is the file being filtered, and the second parameter is the file name being filtered. Filenamefilter. files that return false by accept will be filtered out.
This method returns a file that matches the conditions specified by filenamefilter.
Public file [] listfiles (Java. Io. filefilter)
Public Boolean accept (File file) method. The first parameter file is the file being filtered. Files whose filefilter. ACCEPT returns false will be filtered out.
This method returns a file that matches the conditions specified by the filefilter.
The following example illustrates the usage of the last two methods.
Obtain the list of objects with the specified extension:

Public static filenamefilter getfileextensionfilter (string extension ){
  1. Final string _ extension = extension;
  2. Return new filenamefilter (){
  3. Public Boolean accept (File file, string name ){
  4. Boolean ret = Name. endswith (_ extension );
  5. Return ret;
  6. }
  7. };
  8. }
  9. File file = new file ("C :\\");
  10. File [] zipfiles = file. listfiles (getfileextensionfilter (". Zip "));
    public static FilenameFilter getFileExtensionFilter(String extension) {          final String _extension = extension;          return new FilenameFilter() {              public boolean accept(File file, String name) {                  boolean ret = name.endsWith(_extension);                   return ret;              }          };      }        File file = new File("c:\\");      File[] zipFiles = file.listFiles(getFileExtensionFilter(".zip"));

Obtains the list of objects whose names meet the specified rule expression.
Public static filenamefilter getfileregexfilter (string RegEx ){

  1. Final string RegEx _ = RegEx;
  2. Return new filenamefilter (){
  3. Public Boolean accept (File file, string name ){
  4. Boolean ret = Name. Matches (RegEx _);
  5. Return ret;
  6. }
  7. };
  8. }
  9. File file = new file ("C :\\");
  10. // The obtained file name is 8 numbers and the extended file name is .html
  11. File [] numberhtmlfiles = file. listfiles (getfileregexfilter ("[0-9] {8} \. html "));
Public static filenamefilter getfileregexfilter (string RegEx) {final string RegEx _ = RegEx; return New filenamefilter () {public Boolean accept (File file, string name) {Boolean ret = Name. matches (RegEx _); return ret ;};} file = new file ("C: \"); // extend the file named .html to file [] numberhtmlfiles = file. listfiles (getfileregexfilter ("[0-9] {8 }\\. html "));

Obtain the list of non-directory files:
Public static filefilter getnotdirfilefilter (){

  1. Return new filefilter (){
  2. Public Boolean accept (File file ){
  3. Return file. isfile ();
  4. }
  5. };
  6. }
  7. File file = new file ("C :\\");
  8. File [] notdirfiles = file. listfiles (getnotdirfilefilter ());
Related Article

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.