Data stored in variables , arrays, and objects is temporary and is lost when the program terminates. To permanently store the data created in a program, you need to save them in a file on a disk or a CD. The file can be transported and can be read later by other programs. Since data are stored in files, this section introduces how to use the File class to obtain file properties and to delete and rename files. The next section introduces how to read/write data from/to text files.
Every file is placed in a directory in the file system. An absolute file name contains a file name with its complete path and drive letter. For example, c:\book\Welcome.java is the absolute file name for the file Welcome.java on the Windows operating system. Here c:\book is referred to as the directory path for the file. Absolute file names are machine-dependent . On Unix, the absolute file name may be /home/liang/book/Welcome.java , where /home/liang/book is the directory path for the file Welcome.java .
The File class is intended to provide an abstraction that deals with most of the machine-dependent complexities of files and path names in a machine-independent fashion. The File class contains the methods for obtaining file properties and for renaming and deleting files, as shown in Figure 8.12. However, the File class does not contain the methods for reading and writing file contents.
The filename is a string. The File class is a wrapper class for the file name and its directory path. For example, new File("c:\\book") creates a File object for the directory c:\book , and new File("c:\\book\\test.dat") creates a File object for the file c:\\book\\test.dat , both on Windows. You can use the File class's isDirectory() method to check whether the object represents a directory, and the isFile() method to check whether the object represents a file name.
Caution
The directory separator for Windows is a backslash ( \ ). The backslash is a special character in Java and should be written as \\ in a string literal (see Table 2.5). |
Note
Constructing a File instance does not create a file on the machine. You can create a File instance for any filename regardless of whether it exists or not. You can invoke the exists() method on a File instance to check whether the file exists. |
Do not use absolute file names in your program. If you use a file name such as "c:\\book\\Welcome.java" , it will work on Windows but not on other platforms. You should use a file name relative to the current directory. For example, you may create a File object using new File("Welcome.java") for the file Welcome.java in the current directory. You may create a File object using new File("image/us.gif") for the file us.gif under the image directory in the current directory. The forward slash ( / ) is the Java directory separator, which is the same as on Unix. The statement new File("image/us.gif") works on Windows, Unix, or any other platform.
Listing 8.5 demonstrates how to create a File object and use the methods in the File class to obtain its properties. The program creates a File object for the file us.gif . This file is stored under the image directory in the current directory.
1 public class TestFileClass { 2 public static void main(String[] args) { 3 java.io.File file = new java.io.File( "image/us.gif" ); 4 System.out.println( "Does it exist? " + file.exists() ); 5 System.out.println( "Can it be read? " + file.canRead()); 6 System.out.println( "Can it be written? " + file.canWrite()); 7 System.out.println( "Is it a directory? " + file.isDirectory()); 8 System.out.println( "Is it a file? " + file.isFile()); 9 System.out.println( "Is it absolute? " + file.isAbsolute()); 10 System.out.println( "Is it hidden? " + file.isHidden()); 11 System.out.println( "Absolute path is " + 12 file.getAbsolutePath()); 13 System.out.println( "Last modified on " + 14 new java.util.Date(file.lastModified())); 15 } 16 } |
The lastModified() method returns the date and time when the file was last modified, measured in milliseconds since the Unix time (00:00:00 GMT, January 1, 1970). The Date class is used to display it in a readable format in lines 13 “14.
Figure 8.13(a) shows a sample run of the program on Windows, and Figure 8.13(b), a sample run on Unix. As shown in the figures, the path-naming conventions on Windows are different from those on Unix.