java.io package -Jonathan Knudsen, Patrick Niemeyer

 Input/Output Facilities :

The class hierarchy of the java.io package.


The java.io package


these classes are all subclasses of the basic InputStreamOutputStreamReader, and Writer classes

All fundamental I/O in Java is based on streams. A stream represents a flow of data, or a channel of communication with (at least conceptually) a writer at one end and a reader at the other. When you are working with terminal input and output, reading or writing files, or communicating through sockets in Java, you are using a stream of one type or another. So that you can see the forest without being distracted by the trees, we’ll start by summarizing the classes involved with the different types of streams:

InputStream/OutputStream

Abstract classes that define the basic functionality for reading or writing an unstructured sequence of bytes. All other byte streams in Java are built on top of the basic InputStream and OutputStream.

Reader/Writer

Abstract classes that define the basic functionality for reading or writing a sequence of character data, with support for Unicode. All other character streams in Java are built on top of Reader and Writer.

InputStreamReader/ OutputStreamWriter

“Bridge” classes that convert bytes to characters and vice versa. Remember: in Unicode, a character is not a byte!

DataInputStream/ DataOutputStream

Specialized stream filters that add the ability to read and write simple data types, such as numeric primitives and String objects, in a universal format.

ObjectInputStream/ObjectOutputStream

Specialized stream filters that are capable of writing serialized Java objects and reconstructing them.

BufferedInputStream/BufferedOutputStream/BufferedReader/BufferedWriter

Specialized stream filters that add buffering for additional efficiency.

PrintWriter

specialized character stream that makes it simple to print text.

PipedInputStream/PipedOutputStream/PipedReader/PipedWriter

“Double-ended” streams that normally occur in pairs. Data written into a PipedOutputStream or PipedWriter is read from its corresponding PipedInputStream or PipedReader.

FileInputStream/FileOutputStream/FileReader/FileWriter

Implementations of InputStreamOutputStreamReader, and Writerthat read from and write to files on the local filesystem.

Streams in Java are one-way streets. The java.io input and output classes represent the ends of a simple stream, as shown in Figure 10.2. For bidirectional conversations, we use one of each type of stream.

Basic input and output stream functionality
InputStream and OutputStream are abstract classes that define the lowest-level interface for all byte streams. They contain methods for reading or writing an unstructured flow of byte-level data. Because these classes are abstract, you can’t create a generic input or output stream. Java implements subclasses of these for activities like reading from and writing to files and communicating with sockets. Because all byte streams inherit the structure of InputStream or OutputStream, the various kinds of byte streams can be used interchangeably. A method specifying an InputStream as an argument can, of course, accept any subclass of InputStream. Specialized types of streams can also be layered to provide features, such as buffering, filtering, or handling larger data types.

In Java 1.1, new classes based around Reader and Writer were added to thejava.io package. Reader and Writer are very much like InputStream and OutputStream, except that they deal with characters instead of bytes. As true character streams, these classes correctly handle Unicode characters, which was not always the case with the byte streams. However, some sort of bridge is needed between these character streams and the byte streams of physical devices like disks and networks. InputStreamReader and OutputStreamWriter are special classes that use an encoding scheme to translate between character and byte streams.






















Comments