In order to correctly specify a file’s location, it is necessary to know a little about how files are stored on your computer’s disk drive. File systems are organized into a hierarchy. A
path is a description of a file’s location in the hierarchy. For example, consider the hierarchy of files in
Figure 11.4.1.
Assume that your Java program is named
MyClass.class
. When a program is running, the program’s directory is considered the
current directory. Any files located in the current directory can be referred to by name alone—for example,
MyClass.java
. To refer to a file located in a subdirectory of the current directory, you need to provide the name of the subdirectory and the file:
datafiles/data.txt
. In this case, we are assuming a Linux file system, so we are using the
/
as the separator between the name of the directory (
datafiles
) and the name of the file (
data.txt
). This is an example of a
relative path name, because we are specifying a file in relation to the current directory.
Alternatively, a file can be specified by its
absolute path name. This would be a name whose path starts at the root directory of the file system. For example,
would be the absolute path name for the file named
data.txt
on a Linux system. When you supply the name of a file to one of the stream constructors, you are actually providing a
path name. If the path consists of just a name, such as
data.txt
, Java assumes that the file is located in the same directory as the program itself.