Low-Level and High-Level Streams


Low-level streams. An input or output stream that connects directly to a data source, such as a file or socket.

High-level streams. An input or output stream that reads or writes to another input or output stream.

Low Level Streams Example

FileInputStream and FileOutputStream. For writing and reading binary data from a file.

ByteArrayInputStream and ByteArrayOutputStream. For writing and reading data from an array of bytes.

PipedInputStream and PipedOutputStream. For writing and reading data between two threads.

InputStream and OutputStream. The abstract parent classes

Lets take an example, say stream class is like a pipe. Pipe are of different types. Pipes can be thick , thin ,rubber pipe, plastic pipe, metal pipe etc , different pipe are used for different purpose. It is not efficient to use same type of pipe for all purpose. According to our requirement we use pipe. Same thing happen in case of reading and writing in java.

Like we have different kinds of files i.e binary file(.class) or character file(.java) and different type of data stored in files. So we can't read or write all type file and data (primary data, binary data) by using only one class. That’s why we have different classes in java.io package to read or write.

Moreover sometime we connect different pipes together for efficiency sometime we use some type of filters in pipes. In the same way here we use connect Low Level Stream to High Level Stream and also use BufferedReader and BufferedWriter class as filter classes for improving performance.

High Level Streams Example

BufferedInputStream and BufferedOutputStream. This is used for buffering input and output.

DataInputStream and DataOutputStream.This is a filter for writing and reading primitive data types and Strings.

ObjectInputStream and ObjectOutputStream. This is used for serialization and deserialization of Java objects.

PushbackInputStream.This represents a stream that allows data to be read from the stream, then pushed back into the stream if necessary.

AudioInputStream.This is used for reading audio from an input stream. This class is in the javax.sound.sampled package.

DataInputStream and DataOutputStream.This is a filter for writing and reading primitive data types and Strings.

CheckedInputStream and CheckedOutputStream.This is used for filtering data using a checksum that can be used to verify the data. These classes are found in the java.util.zip package.

CypherInputStream and CypherOutputStream.This is used for working with encrypted data. These classes are found in the javax.crypto package.

ZipInputStream and ZipOutputStream.This is used for working with ZIP files. These classes are found in the java.util.zip package.

JarInputStream and JarOutputStream. This is used for reading and writing to JAR files. These classes are found in the java.util.jar package.

ProgressMonitorInputStream. This monitors the progress of an input stream, and displays a dialog window to the user if the input is taking too long, allowing the user to cancel the input stream. This class is in the javax.swing package.

FAQ

Q: Why make the distinction between low-level and high-level streams? Why not just use the stream you want?

A: Because you often need to use both a low-level stream and one or more high-level streams. The high-level streams perform the type of I/O that you typically need done in an application, similarly to reading and writing primitive data types or objects, while the low-level stream communicates the actual source of the data, similarly to the file or network socket.

Q: But if I want to read from a file, don’t I need to use the low-level FileInputStream class?

A: Yes, if the data in the file is to be read in as bytes. (If it contains character data, you would likely use the FileReader class.)

Q: But the FileInputStream class only reads in data as bytes, so can I only read in bytes at a time?

A: No, you can take the FileInputStream and chain it to a high-level filter like the DataInputStream, which converts the bytes into primitive data types.