Team LiB
Previous Section Next Section

5.8. Files and Directories

The class represents a file or a directory and defines a number of important methods for manipulating files and directories. Note, however, that none of these methods allow you to read the contents of a file; that is the job of, which is just one of the many types of I/O streams used in Java and discussed in the next section. Here are some things you can do with File:

import java.util.*;

// Get the name of the user's home directory and represent it with a File
File homedir = new File(System.getProperty("user.home"));
// Create a File object to represent a file in that directory
File f = new File(homedir, ".configfile");

// Find out how big a file is and when it was last modified
long filelength = f.length();
Date lastModified = new java.util.Date(f.lastModified());

// If the file exists, is not a directory, and is readable, 
// move it into a newly created directory.
if (f.exists() && f.isFile() && f.canRead()) {       // Check config file
  File configdir = new File(homedir, ".configdir"); // A new config directory
  configdir.mkdir();                                // Create that directory
  f.renameTo(new File(configdir, ".config"));       // Move the file into it

// List all files in the home directory
String[] allfiles = homedir.list();

// List all files that have a ".java" suffix
String[] sourcecode = homedir.list(new FilenameFilter() {
  public boolean accept(File d, String name) { return name.endsWith(".java"); }

The File class gained some important additional functionality as of Java 1.2:

// List all filesystem root directories; on Windows, this gives us
// File objects for all drive letters (Java 1.2 and later). 
File[] rootdirs = File.listRoots();

// Atomically, create a lock file, then delete it (Java 1.2 and later)
File lock = new File(configdir, ".lock");
if (lock.createNewFile()) {
  // We successfully created the file.  Now arrange to delete it on exit

  // Now run the application secure in the knowledge that no one else
  // is running it at the same time
else {
  // We didn't create the file; someone else has a lock
  System.err.println("Can't create lock file; exiting.");

// Create a temporary file to use during processing (Java 1.2 and later)
File temp = File.createTempFile("app", ".tmp");  // Filename prefix and suffix
// Do something with the temp file
// And delete it when we're done

5.8.1. RandomAccessFile

The package also defines a RandomAccessFile class that allows you to read binary data from arbitrary locations in a file. This can be useful in certain situations, but most applications read files sequentially, using the stream classes described in the next section. Here is a short example of using RandomAccessFile:

// Open a file for read/write ("rw") access
File datafile = new File(configdir, "datafile");
RandomAccessFile f = new RandomAccessFile(datafile, "rw");;                   // Move to byte 100 of the file
byte[] data = new byte[100];   // Create a buffer to hold data;                  // Read 100 bytes from the file
int i = f.readInt();           // Read a 4-byte integer from the file;                   // Move back to byte 100
f.writeInt(i);                 // Write the integer first
f.write(data);                 // Then write the 100 bytes
f.close();                     // Close file when done with it

    Team LiB
    Previous Section Next Section