Java Input/Output  «Prev   Next»

Lesson 5Files
ObjectiveFile Class to access Files

File Class to Access Files

The File class provides a way to deal with the file system as platform-independently as possible.
Instances of File represent nodes in the file system and either files or directories.
When you create an instance of class File, you specify a directory or file that you would like this instance to represent.
Specifying this directory or file must be accomplished by specifying the absolute or relative path, which can, unfortunately, be platform-specific. For example, whether you use forward slashes, backward slashes, or colons to indicate directory hierarchies depends on whether you are using the Unix, Windows, or Mac operating system.
However, the File class does provide some ways around these types of problems. For example, there is a class variable called pathSeparatorChar that contains the appropriate separator for the OS on which your program is running.
There are also methods for testing whether or not a file exists, for creating a new directory, for testing whether you can read from or write to a particular file, and for deleting a file. Check out the File class in the APIs for more information on this class.

Reading File - Exercise

This exercise leads you through reading from a file.
Reading File - Exercise
Once you have reached the end of the I/O section, refresh what you have learned with a few simple questions.
Java IO - Quiz