File: listFiles(FilenameFilter filter) : File « java.io « Java by API






File: listFiles(FilenameFilter filter)

  
/*
 * Output:
 The 0 matching items in the directory, , are:

 */

import java.io.File;
import java.io.FilenameFilter;
import java.util.Date;

public class MainClass {
  public static void main(String[] args) {
    File myDir = new File("C:/");
    // Define a filter for java source files beginning with F
    FilenameFilter select = new FileListFilter("F", "java");

    File[] contents = myDir.listFiles(select);

    if (contents != null) {
      System.out.println("\nThe " + contents.length
          + " matching items in the directory, " + myDir.getName()
          + ", are:");
      for (File file : contents) {
        System.out.println(file + " is a "
                + (file.isDirectory() ? "directory" : "file")
                + " last modified on\n"
                + new Date(file.lastModified()));
      }
    } else {
      System.out.println(myDir.getName() + " is not a directory");
    }
    return;
  }
}

class FileListFilter implements FilenameFilter {
  private String name; 

  private String extension; 

  public FileListFilter(String name, String extension) {
    this.name = name;
    this.extension = extension;
  }

  public boolean accept(File directory, String filename) {
    boolean fileOK = true;

    if (name != null) {
      fileOK &= filename.startsWith(name);
    }

    if (extension != null) {
      fileOK &= filename.endsWith('.' + extension);
    }
    return fileOK;
  }
}

           
         
    
  








Related examples in the same category

1.File.pathSeparatorChar
2.File.separator
3.File: separatorChar
4.new File(String name)
5.new File(URI uri)
6.new File(String parent, String child)
7.File: canRead()
8.File: canWrite()
9.File: createNewFile()
10.File: createTempFile(String prefix, String suffix)
11.File: delete()
12.File: deleteOnExit()
13.File: exists()
14.File: getAbsoluteFile()
15.File: getAbsolutePath()
16.File: getCanonicalPath()
17.File: getFreeSpace()
18.File: getName()
19.File: getParent()
20.File: getParentFile()
21.File: getPath()
22.File: getTotalSpace()
23.File: getUsableSpace()
24.File: isAbsolute()
25.File: isDirectory()
26.File: isFile()
27.File: listFiles()
28.File: isHidden()
29.File: lastModified()
30.File: length()
31.File: list()
32.File: listRoots()
33.File: mkdir()
34.File: mkdirs() (Creates a directory including any parent directories)
35.File: renameTo(File newFile)
36.File: setLastModified(long time)
37.File: setReadOnly()
38.File: setWritable(boolean writable)
39.File: toURI()
40.File: toURL()