Java Reference
In-Depth Information
}
lcr.close();
System.out.println("\n\nReading luci4.txt using " +
"LowerCaseReader and BufferedReader:");
BufferedReader br = new BufferedReader(
new LowerCaseReader(new FileReader(fileName)));
String str = null;
while ((str = br.readLine()) != null) {
System.out.println(str);
}
br.close();
}
}
Reading luci4.txt using LowerCaseReader:
and now we reach'd the orchard-plot;
and, as we climb'd the hill,
the sinking moon to lucy's cot
came near and nearer still.
Reading luci4.txt using LowerCaseReader and BufferedReader:
and now we reach'd the orchard-plot;
and, as we climb'd the hill,
the sinking moon to lucy's cot
came near and nearer still.
Random Access Files
A
FileInputStream
lets you read data from a file whereas a
FileOutputStream
lets you write data to a file. A random
access file is a combination of both. Using a random access file, you can read from a file as well as write to the file.
Reading and writing using the file input and output streams are a sequential process. Using a random access file, you
can read or write at any position within the file (hence the name random access).
An object of the
RandomAccessFile
class facilitates the random file access. It lets you read/write bytes and all
primitive types values to a file. It also lets you work with strings using its
readUTF()
and
writeUTF()
methods. The
RandomAccessFile
class is not in the class hierarchy of the
InputStream
and
OutputStream
classes.
A random access file can be created in four different access modes. In its constructor, you must specify the access
mode. The access mode value is a string. They are listed as follows:
"r"
: The file is opened in a read-only mode. You will receive an
IOException
if you attempt to
write to the file.
•
"rw"
: The file is opened in a read-write mode. The file is created if it does not exist.
•
"rws"
: Same as the
"rw"
mode, except that any modifications to the file's content and its
metadata are written to the storage device immediately.
•
"rwd"
: Same as the
"rw"
mode, except that any modifications to the file's content are written to
the storage device immediately.
•