Java Reference
In-Depth Information
your binary data files ordinarily must be read only on the same type of computer, and
with the same programming language, as the computer that created that file.
The advantage of binary files is that they are more efficient to process than text files.
Unlike other programming languages, Java also gives its binary files some of the advan-
tages of text files. In particular, Java binary files are platform-independent; that is, with
Java, you can move your binary files from one type of computer to another and your
Java programs will still be able to read the binary files. This combines the portability of
text files with the efficiency of binary files.
The one big advantage of text files is that you can read and write to them using a text
editor. With binary files, all the reading and writing must normally be done by a program.
Text Files Versus Binary Files
Files that you write and read using an editor are called
. Binary files represent data in
a way that is not convenient to read with a text editor, but that can be written to and read
from a program very efficiently.
text files
Self-Test Exercises
1. A stream is a flow of data. From where and to where does the data flow in
an input stream? From where and to where does the data flow in an output
stream?
2. What is the difference between a binary file and a text file?
10.2
Text Files
Polonius: What do you read, my lord?
Hamlet: Words, words, words.
WILLIAM SHAKESPEARE,
Hamlet
In this section, we describe the most common ways to do text file I/O in Java.
Writing to a Text File
The class
is the preferred stream class for writing to a text file. An object of
PrintWriter
PrintWriter
the class
has the methods
and
, which are like the methods
PrintWriter
print
println
and
that you have been using for screen output,
System.out.print
System.out.println
but with an object of the class
, the output goes to a text file. Display 10.1
PrintWriter
Search WWH ::




Custom Search