Java Reference
In-Depth Information
name identifies the final directory name or the filename portion of the path-
name.
The accept() methodusestheseargumentstodeterminewhetherornotthefileor
directorysatisfiesitscriteriaforwhatisacceptable.Itreturnstruewhenthefile/direct-
ory name should be included in the returned array; otherwise, this method returns false.
Listing 8-4 presents a Dir (ectory) application that uses
list(FilenameFilter) toobtainonlythosenamesthatendwithaspecificexten-
sion.
Listing 8-4. Listing specific names
import java.io.File;
import java.io.FilenameFilter;
class Dir
{
public static void main(final String[] args)
{
if (args.length != 2)
{
System.err.println("usage: java Dir dirpath ext");
return;
}
File file = new File(args[0]);
FilenameFilter fnf = new FilenameFilter()
{
public boolean accept(File
dir, String name)
{
return
name.endsWith(args[1]);
}
};
String[] names = file.list(fnf);
for (String name: names)
System.out.println(name);
 
Search WWH ::




Custom Search