TheFile
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 theFile
object to the constructor of some classes, such asFileWriter
.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
AFile
object contains the file name string used to construct it. That string never changes throughout the lifetime of the object. A program can use theFile
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 invocationThe 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 directoryFile a = new File("xanadu.txt");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, usingFile.separator
to specify the file name in a platform-independent way.AlthoughFile b = new File(".." + File.separator + "examples" + File.separator + "xanadu.txt");b
refers to the same file asa
, 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 considera
andb
to be the same. Even though they refer to the same file, the names used to construct them is different.The
FileStuff
example createsFile
objects from names passed from the command line and exercises various information methods on them. You'll find it instructive to runFileStuff
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 passingFileStuff
a variety of relative and absolute path names.Manipulating Files
If aFile
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 thedeleteOnExit
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 ofxanadu.txt
to the current time, a program could donew File("xanadu.txt").setLastModified(new Date().getTime());The
renameTo()
method renames the file. Note that the file name string behind theFile
object remains unchanged, so theFile
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. Themkdirs
method does the same thing, after first creating any parent directories that don't yet exist.The
list
andlistFiles
methods list the contents of a directory. Thelist
method returns an array ofString
file names, whilelistFiles
returns an array ofFile
objects.Static Methods
File
contains some useful static methods.The
createTempFile
method creates a new file with an unique name, and returns aFile
object referring to itThe
listRoots
returns a list of file system root names. On Microsoft Windows, this will be the root directories of mounted drives, such asa:\
andc:\
. On UNIX and Linux systems, this will be the root directory,/
.