pathSeparatorChar is a colon on UNIX systems.
Exercise 20.9: Write a method that, given one or more pathnames, will print all the information available
about the file it represents (if any).
Exercise 20.10: Write a program that uses a StreamTokenizer object to break an input file into words and
counts the number of times each word occurs in the file, printing the result. Use a HashMap to keep track of
the words and counts.
20.7.4. FilenameFilter and FileFilter
The FilenameFilter interface provides objects that filter unwanted files from a list. It supports a single
method:
booleanaccept(File dir, String name)
Returns true if the file named name in the directory dir should be part of
the filtered output.
Here is an example that uses a FilenameFilter object to list only directories:
import java.io.*;
class DirFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return new File(dir, name).isDirectory();
}
public static void main(String[] args) {
File dir = new File(args[0]);
String[] files = dir.list(new DirFilter());
System.out.println(files.length + " dir(s):");
for (String file : files)
System.out.println("\t" + file);
}
}
First we create a File object to represent a directory specified on the command line. Then we create a
DirFilter object and pass it to list. For each name in the directory, list invokes the accept method
on the filtering object and includes the name in the list if the filtering object returns true. For our accept
method, true means that the named file is a directory.
The FileFilter interface is analogous to FilenameFilter, but works with a single File object:
booleanaccept(File pathname)
Returns true if the file represented by pathname should be part of the
filtered output.
Exercise 20.11: Using FilenameFilter or FileFilter, write a program that takes a directory and a
suffix as parameters and prints all files it can find that have that suffix.