Creates and displays a window containing a list of files and sub-directories in a specified directory : Directory « File Input Output « Java

Creates and displays a window containing a list of files and sub-directories in a specified directory


 * Copyright (c) 2004 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 3nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose,
 * including teaching and use in open-source projects.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book, 
 * please visit

import java.awt.Button;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.List;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DateFormat;

 * This class creates and displays a window containing a list of files and
 * sub-directories in a specified directory. Clicking on an entry in the list
 * displays more information about it. Double-clicking on an entry displays it,
 * if a file, or lists it if a directory. An optionally-specified FilenameFilter
 * filters the displayed list.
public class FileLister extends Frame implements ActionListener, ItemListener {
  private List list; // To display the directory contents in

  private TextField details; // To display detail info in.

  private Panel buttons; // Holds the buttons

  private Button up, close; // The Up and Close buttons

  private File currentDir; // The directory currently listed

  private FilenameFilter filter; // An optional filter for the directory

  private String[] files; // The directory contents

  private DateFormat dateFormatter = // To display dates and time correctly
  DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);

   * Constructor: create the GUI, and list the initial directory.
  public FileLister(String directory, FilenameFilter filter) {
    super("File Lister"); // Create the window
    this.filter = filter; // Save the filter, if any

    // Destroy the window when the user requests it
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {

    list = new List(12, false); // Set up the list
    list.setFont(new Font("MonoSpaced", Font.PLAIN, 14));

    details = new TextField(); // Set up the details area
    details.setFont(new Font("MonoSpaced", Font.PLAIN, 12));

    buttons = new Panel(); // Set up the button box
    buttons.setLayout(new FlowLayout(FlowLayout.RIGHT, 15, 5));
    buttons.setFont(new Font("SansSerif", Font.BOLD, 14));

    up = new Button("Up a Directory"); // Set up the two buttons
    close = new Button("Close");

    buttons.add(up); // Add buttons to button box

    this.add(list, "Center"); // Add stuff to the window
    this.add(details, "North");
    this.add(buttons, "South");
    this.setSize(500, 350);

    listDirectory(directory); // And now list initial directory.

   * This method uses the list() method to get all entries in a directory and
   * then displays them in the List component.
  public void listDirectory(String directory) {
    // Convert the string to a File object, and check that the dir exists
    File dir = new File(directory);
    if (!dir.isDirectory())
      throw new IllegalArgumentException("FileLister: no such directory");

    // Get the (filtered) directory entries
    files = dir.list(filter);

    // Sort the list of filenames.

    // Remove any old entries in the list, and add the new ones
    list.add("[Up to Parent Directory]"); // A special case entry
    for (int i = 0; i < files.length; i++)

    // Display directory name in window titlebar and in the details box

    // Remember this directory for later.
    currentDir = dir;

   * This ItemListener method uses various File methods to obtain information
   * about a file or directory. Then it displays that info.
  public void itemStateChanged(ItemEvent e) {
    int i = list.getSelectedIndex() - 1; // minus 1 for Up To Parent entry
    if (i < 0)
    String filename = files[i]; // Get the selected entry
    File f = new File(currentDir, filename); // Convert to a File
    if (!f.exists()) // Confirm that it exists
      throw new IllegalArgumentException("FileLister: " + "no such file or directory");

    // Get the details about the file or directory, concatenate to a string
    String info = filename;
    if (f.isDirectory())
      info += File.separator;
    info += " " + f.length() + " bytes ";
    info += dateFormatter.format(new java.util.Date(f.lastModified()));
    if (f.canRead())
      info += " Read";
    if (f.canWrite())
      info += " Write";

    // And display the details string

   * This ActionListener method is invoked when the user double-clicks on an
   * entry or clicks on one of the buttons. If they double-click on a file,
   * create a FileViewer to display that file. If they double-click on a
   * directory, call the listDirectory() method to display that directory
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == close)
    else if (e.getSource() == up) {
    } else if (e.getSource() == list) { // Double click on an item
      int i = list.getSelectedIndex(); // Check which item
      if (i == 0)
        up(); // Handle first Up To Parent item
      else { // Otherwise, get filename
        String name = files[i - 1];
        File f = new File(currentDir, name); // Convert to a File
        String fullname = f.getAbsolutePath();
        if (f.isDirectory())
          listDirectory(fullname); // List dir
          new FileViewer(fullname).show(); // display file

  /** A convenience method to display the contents of the parent directory */
  protected void up() {
    String parent = currentDir.getParent();
    if (parent == null)

  /** A convenience method used by main() */
  public static void usage() {
    System.out.println("Usage: java FileLister [directory_name] " + "[-e file_extension]");

   * A main() method so FileLister can be run standalone. Parse command line
   * arguments and create the FileLister object. If an extension is specified,
   * create a FilenameFilter for it. If no directory is specified, use the
   * current directory.
  public static void main(String args[]) throws IOException {
    FileLister f;
    FilenameFilter filter = null; // The filter, if any
    String directory = null; // The specified dir, or the current dir

    // Loop through args array, parsing arguments
    for (int i = 0; i < args.length; i++) {
      if (args[i].equals("-e")) {
        if (++i >= args.length)
        final String suffix = args[i]; // final for anon. class below

        // This class is a simple FilenameFilter. It defines the
        // accept() method required to determine whether a specified
        // file should be listed. A file will be listed if its name
        // ends with the specified extension, or if it is a directory.
        filter = new FilenameFilter() {
          public boolean accept(File dir, String name) {
            if (name.endsWith(suffix))
              return true;
              return (new File(dir, name)).isDirectory();
      } else {
        if (directory != null)
          usage(); // If already specified, fail.
          directory = args[i];

    // if no directory specified, use the current directory
    if (directory == null)
      directory = System.getProperty("user.dir");
    // Create the FileLister object, with directory and filter specified.
    f = new FileLister(directory, filter);
    // Arrange for the application to exit when the window is closed
    f.addWindowListener(new WindowAdapter() {
      public void windowClosed(WindowEvent e) {
    // Finally, pop the window up up.;

class FileViewer extends Frame implements ActionListener {
  String directory; // The default directory to display in the FileDialog

  TextArea textarea; // The area to display the file contents into

  /** Convenience constructor: file viewer starts out blank */
  public FileViewer() {
    this(null, null);

  /** Convenience constructor: display file from current directory */
  public FileViewer(String filename) {
    this(null, filename);

   * The real constructor. Create a FileViewer object to display the specified
   * file from the specified directory
  public FileViewer(String directory, String filename) {
    super(); // Create the frame

    // Destroy the window when the user requests it
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {

    // Create a TextArea to display the contents of the file in
    textarea = new TextArea("", 24, 80);
    textarea.setFont(new Font("MonoSpaced", Font.PLAIN, 12));
    this.add("Center", textarea);

    // Create a bottom panel to hold a couple of buttons in
    Panel p = new Panel();
    p.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 5));
    this.add(p, "South");

    // Create the buttons and arrange to handle button clicks
    Font font = new Font("SansSerif", Font.BOLD, 14);
    Button openfile = new Button("Open File");
    Button close = new Button("Close");


    // Figure out the directory, from filename or current dir, if necessary
    if (directory == null) {
      File f;
      if ((filename != null) && (f = new File(filename)).isAbsolute()) {
        directory = f.getParent();
        filename = f.getName();
      } else
        directory = System.getProperty("user.dir");
    } = directory; // Remember the directory, for FileDialog
    setFile(directory, filename); // Now load and display the file

   * Load and display the specified file from the specified directory
  public void setFile(String directory, String filename) {
    if ((filename == null) || (filename.length() == 0))
    File f;
    FileReader in = null;
    // Read and display the file contents. Since we're reading text, we
    // use a FileReader instead of a FileInputStream.
    try {
      f = new File(directory, filename); // Create a file object
      in = new FileReader(f); // And a char stream to read it
      char[] buffer = new char[4096]; // Read 4K characters at a time
      int len; // How many chars read each time
      textarea.setText(""); // Clear the text area
      while ((len = != -1) { // Read a batch of chars
        String s = new String(buffer, 0, len); // Convert to a string
        textarea.append(s); // And display them
      this.setTitle("FileViewer: " + filename); // Set the window title
      textarea.setCaretPosition(0); // Go to start of file
    // Display messages if something goes wrong
    catch (IOException e) {
      textarea.setText(e.getClass().getName() + ": " + e.getMessage());
      this.setTitle("FileViewer: " + filename + ": I/O Exception");
    // Always be sure to close the input stream!
    finally {
      try {
        if (in != null)
      } catch (IOException e) {

   * Handle button clicks
  public void actionPerformed(ActionEvent e) {
    String cmd = e.getActionCommand();
    if (cmd.equals("open")) { // If user clicked "Open" button
      // Create a file dialog box to prompt for a new file to display
      FileDialog f = new FileDialog(this, "Open File", FileDialog.LOAD);
      f.setDirectory(directory); // Set the default directory

      // Display the dialog and wait for the user's response;

      directory = f.getDirectory(); // Remember new default directory
      setFile(directory, f.getFile()); // Load and display selection
      f.dispose(); // Get rid of the dialog box
    } else if (cmd.equals("close")) // If user clicked "Close" button
      this.dispose(); // then close the window

   * The FileViewer can be used by other classes, or it can be used standalone
   * with this main() method.
  static public void main(String[] args) throws IOException {
    // Create a FileViewer object
    Frame f = new FileViewer((args.length == 1) ? args[0] : null);
    // Arrange to exit when the FileViewer window closes
    f.addWindowListener(new WindowAdapter() {
      public void windowClosed(WindowEvent e) {
    // And pop the window up;


Related examples in the same category

1.Create directory
2.Create directory along with required nonexistent parent directories
3.Create directory tree (nested/cascade folders)
4.Create a directories recursively
5.Copying a Directory: Copies files under srcDir to dstDir, if dstDir does not exist, it will be created.
6.Delete a non-empty directory: Deletes all files and subdirectories under dir.
7.Listing the Files or Subdirectories in a Directory
8.Listing the File System Roots
9.The Directory Listing ApplicationThe Directory Listing Application
10.use list( ) to examine the contents of a directory:
11.Reading and Printing a Directory HierarchyReading and Printing a Directory Hierarchy
12.Display a file system in a JTree viewDisplay a file system in a JTree view
13.File Tree DemoFile Tree Demo
14.File Table HTMLFile Table HTML
15.A standalone program that deletes a specified file or directory
16.Get Last modification time of a file or directory
17.Set last modified time of a file or directory
18.List contents of a directory
19.Determine if file or directory exists
20.Determine if File or Directory is hidden
21.Check if a directory is not empty
22.Get name of parent directory
23.Get name of specified file or directory
24.Get current directory
25.Mark file or directory Read Only
26.Rename file or directory
27.Traversing all files and directories under dir
28.Traversing only directories under dir
29.Traversing only files under dir
30.Calculate directory size
31.Delete directory recursively
32.Determining If Two Filename Paths Refer to the Same File
33.Recursive directory deletion
34.Recursivly delete directory
35.Searches through the directory tree
36.Starts at the directory given and tests to see whether it is empty
37.Directory Walker
38.Utility methods for handling files and directories
39.Creates a new and empty directory in the default temp directory using the given prefix.
40.Count files in a directory (including files in all subdirectories)
41.Create a unique directory within a directory 'root'
42.Creates a new empty temporary directory.
43.Get Files Recurse
44.Recursively search a directory tree
45.Find directoriesFind directories