Java - general

File filter

java.io.FileFilter
The following, very simple file filter shows all files which are lying in the current list, which begin with an „a“ and which have the ending „pdf“ (See also: Wildcardsearch):


package com.sowas.javawiki.filefilter;
 
import java.io.*;
 
public class FileFilter {
   final String strPath = ".";
 
   public FileFilter(final String strFilename, final String strExtension) {
      try {
         File file = new File(strPath);
         String[] strFiles = file.list(new FilenameFilter() {
            public boolean accept(File file, String filename) {
               // This simple filter cannot deal with wildcards!
               return filename.startsWith(strFilename) && filename.endsWith("."+strExtension);
            }
         });
         for (int i = 0; i < strFiles.length; i++) {
            System.out.println("file["+i+"]="+strFiles[i]);
         }
      }catch (Exception e){
         e.printStackTrace();
      }
   }
 
   public static void main(String[] args) {  
      new FileFilter("a", "pdf");  // All files with an "a" at the beginning and "pdf" as extension will be showed 
   }
}

Eigene Werkzeuge
Werkzeuge

gratis Counter by GOWEB
seit 9.10.2007