TheFileclass makes it easier to write platform-independent code that examines and manipulates files. The name of this class is misleading:Fileinstances represent file names, not files. The file corresponding to the file name might not even exist.Why create a
Fileobject 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 theFileobject 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
AFileobject contains the file name string used to construct it. That string never changes throughout the lifetime of the object. A program can use theFileobject 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
Fileobject 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.txtxanadu.txta.getName()xanadu.txtxanadu.txtb.getParent()NULLNULLa.getAbsolutePath()c:\java\examples\xanadu.txt/home/cafe/java/examples/xanadu.txta.getCanonicalPath()c:\java\examples\xanadu.txt/home/cafe/java/examples/xanadu.txtThen the same program constructs a
Fileobject from a more complicated file name, usingFile.separatorto specify the file name in a platform-independent way.AlthoughFile b = new File(".." + File.separator + "examples" + File.separator + "xanadu.txt");brefers 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.txtb.getName()xanadu.txtxanadu.txtb.getParent()..\examples../examplesb.getAbsolutePath()c:\java\examples\..\examples\xanadu.txt/home/cafe/java/examples/../examples/xanadu.txtb.getCanonicalPath()c:\java\examples\xanadu.txt/home/cafe/java/examples/xanadu.txtRunning 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 consideraandbto be the same. Even though they refer to the same file, the names used to construct them are different.The
FileStuffexample createsFileobjects from names passed from the command line and exercises various information methods on them. You'll find it instructive to runFileStuffon a variety of file names. Be sure to include directory names as well as the names of files that don't actually exist. Try passingFileStuffa variety of relative and absolute path names.Manipulating Files
If aFileobject 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
deletemethod deletes the file immediately, while thedeleteOnExitmethod deletes the file when the virtual machine terminates.The
setLastModifiedsets the modification date/time for the file. For example, to set the modification time ofxanadu.txtto 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 theFileobject remains unchanged, so theFileobject will not refer to the renamed file.Working with Directories
Filehas some useful methods for working with directories.The
mkdirmethod creates a directory. Themkdirsmethod does the same thing, after first creating any parent directories that don't yet exist.The
listandlistFilesmethods list the contents of a directory. Thelistmethod returns an array ofStringfile names, whilelistFilesreturns an array ofFileobjects.Static Methods
Filecontains some useful static methods.The
createTempFilemethod creates a new file with a unique name and returns aFileobject referring to itThe
listRootsreturns 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,/.