DEV Community

Cover image for The File Class
Paul Ngugi
Paul Ngugi

Posted on

The File Class

The File class contains the methods for obtaining the properties of a file/directory and for renaming and deleting a file/directory. Having learned exception handling, you are ready to step into file processing. Data stored in the program are temporary; they are 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 other permanent storage device. The file can then be transported and read later by other programs. Since data
are stored in files, this section introduces how to use the File class to obtain file/directory properties, to delete and rename files/directories, and to create directories. 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 (or full 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 the UNIX platform, the absolute file name may be /home/liang/book/Welcome.java, where /home/liang/book is the directory path for the file Welcome.java.

A relative file name is in relation to the current working directory. The complete directory path for a relative file name is omitted. For example, Welcome.java is a relative file name. If the current working directory is c:\book, the absolute file name would be c:\book\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 and directory properties and for renaming and deleting files and directories, as shown in Figure below. However, the File class does not contain the methods for reading and writing file contents.

The file name 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.

Image description

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. _Constructing a **File instance does not create a file on the machine._ You can create a File instance for any file name regardless 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, and any other platform.

The program below 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.

Image description

The lastModified() method returns the date and time when the file was last modified, measured in milliseconds since the beginning of UNIX time (00:00:00 GMT, January 1, 1970). The Date class is used to display it in a readable format in lines 16.

Top comments (0)