Principle 11.2.3. Platform Independence.
Java binary files are platform independent. They can be interpreted by any computer that supports Java.
System.out
output stream and and the System.in
input stream (Fig. Figure 11.2.1) in this text’s examples. Recall that System.out
usually connects your program (source) to the screen (destination) and System.in
usually connects the keyboard (source) to the running program (destination). What you have learned about streams will also be a key for connecting files to a program.byte
, a 16-bit short
, a 16-bit char
, a 32-bit int
, a 64-bit long
, a 32-bit float
, or a 64-bit double
. As we know, these are Java’s primitive numeric types. In addition to these aggregates, we can group together a sequence of char
to form a String
.String
s), expected year of graduation (int
), and current grade point average represented by (double
). Collections of these records are typically grouped into files.int
or one record) ends and the next one begins. Therefore, it will be up to us to provide the boundaries as we process the data.*.java
) are text files, and so are the HTML files that populate the World Wide Web. The big advantage of text files is their portability. Because their data are represented in the ASCII code (Table 5.9.1), they can be read and written by just about any text-processing program. Thus, a text file created by a program on a Windows computer can be read by a Macintosh program.java.io
package, which must be imported by any program that does I/O. They are generally organized into the hierarchy illustrated in Figure 11.2.4. We will cover only a small portion of the hierarchy in this text. Generally speaking, binary files are processed by subclasses of InputStream
and OutputStream
. Text files are processed by subclasses of Reader
and Writer
, both of which are streams, despite their names.InputStream
and OutputStream
are abstract classes that serve as the root classes for reading and writing binary data. Their most commonly used subclasses are DataInputStream
and DataOutputStream
, which are used for processing String
data and data of any of Java’s primitive types—char
, boolean
, int
, double
, and so on. The analogues of these classes for processing text data are the Reader
and Writer
classes, which serve as the root classes for all text I/O.DataInputStream
s and DataOutputStream
s normally are used for binary I/O. Reader
and Writer
streams normally are used for text I/O.FileInputStream
and FileOutputStream
are used for performing binary input and output on files. The PrintStream
class contains methods for outputting various primitive data—integers, floats, and so forth—as text. The System.out
stream, one of the most widely used output streams, is an object of this type. The PrintWriter
class, which was introduced in JDK 1.1 contains the same methods as PrintStream
but the methods are designed to support platform independence and internationalized I/O—that is, I/O that works in different languages and alphabets.PrintWriter
are designed to output a particular type of primitive data (Fig. 11.4). As you would expect, there is both a print()
and println()
method for each kind of data that the programmer wants to output.BufferedReader
and File
classes, which were used in Chapter 4.Class | Description |
InputStream |
Abstract root class of all binary input streams |
FileInputStream |
Provides methods for reading bytes from a binary file |
FilterInputStream |
Provides methods required to filter data |
BufferedInputStream |
Provides input data buffering for reading large files |
ByteArrayInputStream |
Provides methods for reading an array as if it were a stream |
DataInputStream |
Provides methods for reading Java’s primitive data types |
PipedInputStream |
Provides methods for reading piped data from another thread |
OutputStream |
Abstract root class of all binary output streams |
FileOutputStream |
Provides methods for writing bytes to a binary file |
FilterOutputStream |
Provides methods required to filter data |
BufferedOutputStream |
Provides output data buffering for writing large files |
ByteArrayOutputStream |
Provides methods for writing an array as if it were a stream |
DataOutputStream |
Provides methods for writing Java’s primitive data types |
PipedOutputStream |
Provides methods for writing piped data to another thread |
PrintStream |
Provides methods for writing primitive data as text |
Reader |
Abstract root class for all text input streams |
BufferedReader |
Provides buffering for character input streams |
CharArrayReader |
Provides input operations on char arrays |
FileReader |
Provides methods for character input on files |
FilterReader |
Provides methods to filter character input |
StringReader |
Provides input operations on String s |
Writer |
Abstract root class for all text output streams |
BufferedWriter |
Provides buffering for character output streams |
CharArrayWriter |
Provides output operations to char arrays |
FileWriter |
Provides methods for output to text files |
FilterWriter |
Provides methods to filter character output |
PrintWriter |
Provides methods for printing binary data as characters |
StringWriter |
Provides output operations to String s |
FilterInputStream
and FilterReader
classes can be used to filter binary and text data during input. Methods in the FilterOutputStream
and FilterWriter
can be used to filter output data. These classes serve as the root classes for various filtering subclasses. They can also be subclassed to perform customized data filtering.BufferedInputStream
and BufferedReader
, for performing binary and text input, and BufferedOutputStream
and BufferedWriter
, for buffered output operations.flush()
method is called.FilterWriter
subclass and override its write()
methods to perform the desired filtering operation. Similarly, to remove the line numbers from such a file during input, you could define a FilterReader
subclass. In that case, you would override its read()
methods to suit your goals for the program.ByteArrayInputStream
, ByteArrayOutputStream
, CharArrayReader
, and CharArrayWriter
are four classes that take input from or send output to arrays in the program’s memory. Methods in these classes can be useful for performing various operations on data during input or output. For example, suppose a program reads an entire line of integer data from a binary file into a ByteArray
. It might then transform the data by, say, computing the remainder modulo N
of each value. The program now can read these transformed data by treating the byte array as an input stream. A similar example would apply for some kind of output transformation.StringReader
and StringWriter
classes provide methods for treating String
s and StringBuffer
s as I/O streams. These methods can be useful for performing certain data conversions.String
by writing it to a StringBuffer
, which can then be output as an entire line of text. StringReader
methods can be used to read integer data from an ordinary String
object.