Listing a Directory


File directory = new File("users/tim"); String[] result = directory.list();



We can also use the File class to list the contents of a directory. In this phrase, we use the list() method of the File class to get a String array containing all the files and subdirectories contained within the directory specified by the File object. If the directory does not exist, a null value is returned. The strings returned are filenames and simple directory names; they are not complete paths. Also, the results are not guaranteed to be in any defined order.

An alternate implementation of the list() method is also available. It accepts a java.io.FilenameFilter parameter and allows you to filter the files and directories returned in the list results. Use of this method is shown here:

File directory = new File("users/tim"); FilenameFilter fileFilter = new HTMLFileFilter(); String[] result = directory.list(fileFilter);


The corresponding implementation of the HTMLFileFilter class used above is shown here:

class HTMLFileFilter extends FilenameFilter {    public boolean accept(File f) {       return f.isDirectory() || f.getName()       .toLowerCase() .endsWith(".html"));    }    public String getDescription() }       return "/html files"; }


FilenameFilter is an interface with one defined method, accept(). The accept() method takes two parametersa File object and a String object. The File object specifies the directory in which the file was found, and the String object specifies the name of the file. The accept() method returns true if the file should be included in the list; otherwise, it returns false. In this example, we have created a filter that will cause the list() method to include only files that end with the .html extention.

If the filter passed in is null, this method behaves identical to the previous list() method with no parameters.

In addition to the list() methods, the File class also has two versions of a listFiles() method. The listFiles() method returns an array of File objects instead of a string array. The no parameter form of this method is shown here:

File directory = new File("users/tim"); File[] result = directory.listFiles();


The resulting File objects contain relative or absolute pathnames, depending on the File object from which the listFiles() method was called. In the previous example, if the directory File object contained an absolute path, the results would contain absolute pathnames. If the directory File object contained a relative pathname, the results would have relative pathnames.

There is also a version of listFiles() that accepts a FileFilter parameter, similar to the example we showed for the list() method. We show an example of this here:

File directory = new File("users/tim"); FileFilter fileFilter = new HTMLFileFilter(); String[] result = directory.listFiles(fileFilter);


The corresponding implementation of the HTMLFileFilter class used above is shown here:

class HTMLFileFilter extends FileFilter {    public boolean accept(File f) {       return f.isDirectory() || f.getName().toLowerCase().endsWith(".html");    }    public String getDescription() {       return ".html files";    } }


FileFilter is an interface with two defined methods, accept() and getdescription(). Unlike the accept() method of FilenameFilter, the accept() method of FileFilter takes only one parameter, a File object. The File object specifies either a file or directory. The accept() method returns true if the file or directory should be included in the list; otherwise, it returns false. In this example, we have created a filter that will cause the list() method to include only files that end with the .html extention and directories.




JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net