Specifying which file to open involves not only the file name but also the extension and the path.
The name of the file is
what Windows Explorer shows you in the right pane of Figure 1. The names
of four files are listed: FilePaths.html
,
FilePaths.jpr
, fName_1.txt
, and
Main.java
.
Figure 1 |
The extensions indicate the kind of data contained in the file. Extensions are the part of the filename after the last period.
html
files contain data suitable for displaying on the
World Wide Web. jpr
files contain project information for JBuilder.txt
files contain text.java
files contain the source code for Java
programs.Finally, the path specifies how to find the file on the disk.
Windows Explorer shows the path beside the word Address. All
four files shown above have the same path:
D:\cs132\W03\L03\filePaths
.
The D:
specifies which of possibly several disk
drives contains the file. Each of the names between slashes specifies a
folder or directory. Each directory, except the first, is contained within
the directory immediately to its left in the path. This is shown
graphically in Windows Explorer's left pane.
Open the FilePaths.jpr
file. When you navigate to it with
Windows Explorer, it should appear as in Figure 1 except for the disk
drive.
Main.java
contains the following code:
package filePaths; import becker.io.TextOutput; public class Main extends Object { public static void main(String[] args) { String pathName = "U:\\cs132\\W03\\L03\\filePaths\\"; String fileName = "fName_1.txt"; System.out.println("pathName = '" + pathName + "'."); System.out.println("fileName = '" + fileName + "'."); TextOutput out = new TextOutput(pathName + fileName); out.println("pathName = '" + pathName + "'."); out.println("fileName = '" + fileName + "'."); out.close(); System.out.println("All done!"); } }
U:
) in the file matches
the drive that your project is on. (I.e. if you're working at home, you
will want to change the U:
to C:
or whatever
drive letter you are saving your work on.)
A backslash like is used in a file path can't be used directly in a Java String
. Java uses the backslash to mean the next character means something special. Doubling the backslash in Java indicates that the next character (the second backslash) should be inserted into theString
.
fName_1.txt
should be created in the same folder as the
project file.fileName
to
fName_2.txt
and the pathName
to
U:\\cs132\\W03\\L03\\
.
A path that starts with a disk drive letter is called an absolute path. An absolute path states exactly where the file will go, no matter which directory contains the main
method.
fileName
to
defDirFile.txt
and pathName
to the
empty String
(""
).FilePaths.jpr
?
The obvious relationship between these two is no accident! The program's current directory is the same directory that contains the project file. If you don't specify a file path, this is where the file will be created.
A file can also be specified relative to the current directory, the
directory containing the project file. Relative paths are like directions
you might give someone: to get there from here, you go north 3
blocks, turn right, go one block, and then go one more block north.
For a relative path, you might say go up two directories, then down
into the directory named data
.
The way to say go up one directory is with a
..
in the path (two dots means just one
directory). The way to say go down a directory is to give the
directory's name.
Do the following:
W03
. In it, create a folder named
data
.pathName
to ..\\..\\data\\
and the
fileName
to fName_3.txt
.File names and paths behave the same way whether you are writing a
file with TextOutput
or reading it with
TextInput
.