Java Reference
In-Depth Information
assumed that the file is in the same directory (folder) as the one in which the program
is run. You can also use a full or relative path name.
A path name not only gives the name of the file, but also tells what directory
(folder) the file is in. A full path name , as the name suggests, gives a complete path
name, starting from the root directory. A relative path name gives the path to the file,
starting with the directory that your program is in. The way that you specify path
names depends on your particular operating system.
A typical UNIX path name is
path names
/user/sallyz/data/data.txt
To create a BufferedReader input stream connected to this file, you use
BufferedReader inputStream =
new BufferedReader( new FileReader("/user/sallyz/data/data.txt"));
Windows uses \ instead of / in path names. A typical Windows path name is
C:\dataFiles\goodData\data.txt
To create a BufferedReader input stream connected to this file, you use
BufferedReader inputStream =
new BufferedReader(
new FileReader("C:\\dataFiles\\goodData\\data.txt"));
Note that you need to use \\ in place of \ , since otherwise Java will interpret a
backslash paired with a character, such as \d , as an escape character. Although you
must worry about using a backslash ( \ ) in a quoted string, this problem does not occur
with path names read in from the keyboard.
One way to avoid these escape-character problems altogether is to always use UNIX
conventions when writing path names. A Java program will accept a path name writ-
ten in either Windows or UNIX format, even if it is run on a computer with an oper-
ating system that does not match the syntax. Thus, an alternate way to create a
BufferedReader input stream connected to the Windows file
using \ ,
\\ , or /
C:\dataFiles\goodData\data.txt
is the following:
BufferedReader inputStream =
new BufferedReader(
new FileReader("C:/dataFiles/goodData/data.txt"));
Nested Constructor Invocations
Expressions with two constructors, such as the following, are common when dealing
with Java's library of I/O classes:
new BufferedReader( new FileReader("original.txt"))
Search WWH ::




Custom Search