Trail: Essential Classes
Lesson: Basic I/O
Section: File I/O
File Objects
Home Page > Essential Classes > Basic I/O
File Objects
The File class makes it easier to write platform-independent code that examines and manipulates files. The name of this class is misleading: File instances represent file names, not files. The file corresponding to the file name might not even exist.

Why create a File object for a file that doesn't exist? A program can use the object to parse a file name. Also, the file can be created by passing the File object to the constructor of some classes, such as FileWriter.

If the file does exist, a program can examine its attributes and perform various operations on the file, such as renaming it, deleting it, or changing its permissions.

A File Has Many Names

A File object contains the file name string used to construct it. That string never changes throughout the lifetime of the object. A program can use the File object to obtain other versions of the file name, some of which may or may not be the same as the original file name string passed to the constructor.

Suppose a program creates a File object with the constructor invocation

File a = new File("xanadu.txt");
The program invokes a number of methods to obtain different versions of the file name. The program is then run both on a Microsoft Windows system (in directory c:\java\examples) and a Solaris system (in directory /home/cafe/java/examples). Here is what the methods would return:

Method Invoked Returns on Microsoft Windows Returns on Solaris
a.toString() xanadu.txt xanadu.txt
a.getName() xanadu.txt xanadu.txt
b.getParent() NULL NULL
a.getAbsolutePath() c:\java\examples\xanadu.txt /home/cafe/java/examples/xanadu.txt
a.getCanonicalPath() c:\java\examples\xanadu.txt /home/cafe/java/examples/xanadu.txt

Then the same program constructs a File object from a more complicated file name, using File.separator to specify the file name in a platform-independent way.

File b = new File(".." + File.separator + "examples" + File.separator + "xanadu.txt");
Although b refers to the same file as a, the methods return slightly different values:

Method Invoked Returns on Microsoft Windows Returns on Solaris
b.toString() ..\examples\xanadu.txt ../examples/xanadu.txt
b.getName() xanadu.txt xanadu.txt
b.getParent() ..\examples ../examples
b.getAbsolutePath() c:\java\examples\..\examples\xanadu.txt /home/cafe/java/examples/../examples/xanadu.txt
b.getCanonicalPath() c:\java\examples\xanadu.txt /home/cafe/java/examples/xanadu.txt

Running the same program on a Linux system would give results similar to those on the Solaris system.

It's worth mentioning that File.compareTo()would not consider a and b to be the same. Even though they refer to the same file, the names used to construct them are different.

The FileStuff example creates File objects from names passed from the command line and exercises various information methods on them. You'll find it instructive to run FileStuff on a variety of file names. Be sure to include directory names as well as the names of files that don't actually exist. Try passing FileStuff a variety of relative and absolute path names.

Manipulating Files

If a File object names an actual file, a program can use it to perform a number of useful operations on the file. These include passing the object to the constructor for a stream to open the file for reading or writing.

The delete method deletes the file immediately, while the deleteOnExit method deletes the file when the virtual machine terminates.

The setLastModified sets the modification date/time for the file. For example, to set the modification time of xanadu.txt to the current time, a program could do

new File("xanadu.txt").setLastModified(new Date().getTime());

The renameTo() method renames the file. Note that the file name string behind the File object remains unchanged, so the File object will not refer to the renamed file.

Working with Directories

File has some useful methods for working with directories.

The mkdir method creates a directory. The mkdirs method does the same thing, after first creating any parent directories that don't yet exist.

The list and listFiles methods list the contents of a directory. The list method returns an array of String file names, while listFiles returns an array of File objects.

Static Methods

File contains some useful static methods.

The createTempFile method creates a new file with a unique name and returns a File object referring to it

The listRoots returns a list of file system root names. On Microsoft Windows, this will be the root directories of mounted drives, such as a:\ and c:\. On UNIX and Linux systems, this will be the root directory, /.

Previous page: File I/O
Next page: Random Access Files