Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Listing a Directory

Published? true
FormatLanguage: WikiFormat

Problem:

You need to list the filesystem entries named in a directory.

Solution:

Use a java.io.File object’s list() or listFiles() method.

Discussion:

The java.io.File class contains several methods for working with directories. For example, to list the filesystem entities named in the current directory, just write:

String[] list = new File(".").list()

To get an array of already constructed File objects rather than Strings, use:

File[] list = new File(".").listFiles();

You can display the result in a ListView.

Of course, there’s lots of room for elaboration. You could print the names in multiple columns across or down the screen in a TextView in a monospace font, since you know the number of items in the list before you print. You could omit filenames with leading periods, as does the Unix ls program. Or print the directory names first; as some "file manager" type programs do. By using listFiles(), which constructs a new File object for each name, you could print the size of each, as per the DOS dir command or the Unix ls -l command (see Getting File Information). Or you could figure out whether each is a file, a directory, or neither. Having done that, you could pass each directory to your top-level function, and you’d have directory recursion (the Unix find command, or ls -R, or the DOS DIR /S command). Quite the makings of a file manager application of your own.

A more flexible way to list filesystem entries is with list(FilenameFilter ff). FilenameFilter is a tiny interface with only one method: boolean accept(File inDir, String fileName). Suppose you want a listing of only Java-related files (*.java, *.class, *.jar, etc.). Just write the accept() method so that it returns true for these files and false for any others. Here is the Ls class warmed over to use a FilenameFilter instance

import java.io.*;

/**
 * FNFilter - directory lister modified to use FilenameFilter
 */
public class FNFilter {
 public static String[] getListing(String startingDir) {
 // Generate the selective list, with a one-use File object.
 String[] dir = new java.io.File(startingDir).list(new OnlyJava());
 java.util.Arrays.sort(dir); // Sort it (Data Structuring chapter))
 return dir;
}

/** FilenameFilter implementation: 
 * The Accept method only returns true for .java , .jar and class files.
 */
class OnlyJava implements FilenameFilter {
 public boolean accept(File dir, String s) {
 if (s.endsWith(".java") || s.endsWith(".dex") || s.endsWith(".jar"))
 return true;
 // others: projects, ... ?
 return false;
 }
}

The FilenameFilter could be more flexible; in a full-scale application, the list of files returned by the FilenameFilter would be chosen dynamically, possibly automatically, based on what you were working on. File Chooser dialogs implement this as well, allowing the user to select interactively from one of several sets of files to be listed. This is a great convenience in finding files, just as it is here in reducing the number of files that must be examined.

For the listFiles() method, there is an additional overload that accepts a FileFilter. The only difference is that FileFilter's accept method is called with a File object, whereas FileNameFilter's is called with a filename String.

See Also:

See Building list-based applications with ListView to display the results in your GUI. Chapter 11 of the Java Cookbook has more information on file and directory operations.