3.2.6 File Management3.2.5.4 Putting all Together3.2 Streams and FilesLiterature

3.2.6 File Management

There is more to file management than reading and writing. The class File covers functionality for manipulating the files in a file system:

   public class File
     public static final String separator;

     public File(String name);
     public File(String dir, String name);
     public File(File dir, String name);

     public String toString();
     public String getName();
     public String getPath();

     public boolean exists();
     public boolean canRead();
     public boolean canWrite();

     public boolean isFile();
     public boolean isDirectory();

     public boolean mkdir();
     public boolean delete();
     public boolean renameTo(File dest);

     public File[]  listFiles();

An object of class File denotes an  abstract pathname for a file or directory. Creating a File object does not yet result in the creation of the corresponding file in the file system; it only gives the possibility to manipulate a file with the corresponding name.

The simplest constructor for a File object just takes the name of the file (if no path is specified, the current directory is assumed):

  File file = new File("test.txt");

The file stream classes FileReader, FileWriter, FileInputStream, and FileOutputStream also have constructors that take File (rather than String) objects as arguments. Thus we may for instance create a FileWriter stream as

   FileWriter writer = new FileWriter(file);

We may also create a File object with a specific name in a specific parent directory, e.g.

  File file = new File("/usr/home/schreine", "test.txt");

The resulting path is the composition of both components with the File constant separator inserted. This constant represents the  directory separator character of the underlying machine, e.g., "\" in MS Windows and "/" in Unix/Linux. For portability, one should never hardwire this character in a program but rather use this constant.  

The method toString converts a File object (back) to a String; the methods getName and getPath return the file component and the directory component of a file path.

We can test whether a file actually exists by

  boolean exists = file.exists();

For further information, we have the methods canRead (whether we have permission to read the file), canWrite (whether we have permission to write the file), isFile (whether it is a normal file), isDirectory (whether it is actually a directory).

If a File object file denotes a directory path, we may call

  boolean okay = file.mkDir();

where the return value signals whether the call was successful or not. Correspondingly, we may delete a file or directory with delete, and rename a file or directory by a call of renameTo.

If a File object denotes a directory, we may use listFiles to get an array of all the files/subdirectories contained in this directory. The use of this method is demonstrated by the following example.

Example  We write a method listDir that takes as its argument a directory path and recursively lists the content of this directory and its subdirectories. Directories and subdirectories are appropriately indented, e.g.

The following method delegates this task to a function list to which the current File object and the indentation level is passed:

    static void listDir(String path)
      File file = new File(path);
      if (!file.exists()) return;
      list(file, 0);

Method list is defined recursively. First the name of the file is printed with appropriate indentation. If the file is not a directory or we cannot read it, we are done. Otherwise, we retrieve the contents of the directory and call list recursively with each file (subdirectory) in turn:  

    static void list(File file, int indent)
      for(int i=0; i<indent; i++) System.out.print("   ");

      if (!file.isDirectory() || !file.canRead()) return;

      File[] files = file.listFiles();
      int n = files.length;
      for (int i=0; i<n; i++)
        list(files[i], indent+1);

© Wolfgang Schreiner; February 3, 2005

3.2.6 File Management3.2.5.4 Putting all Together3.2 Streams and FilesLiterature