Java byte streams and character streams

Introduction

Java defines two types of streams:

  • byte
  • character

Byte streams are used to handle input and output of bytes. For example, reading or writing binary data.

Character streams are used to handle input and output of characters. They use Unicode and can be internationalized.

Byte Stream Classes

Byte streams are defined by using two class hierarchies.

At the top are two abstract classes:

  • InputStream
  • OutputStream

The following table lists several stream classes.

Stream Class Meaning
BufferedInputStream Buffered input stream
BufferedOutputStream Buffered output stream
ByteArrayInputStream reads from a byte array
ByteArrayOutputStreamwrites to a byte array
DataInputStream contains methods for reading the Java standard data types
DataOutputStream contains methods for writing the Java standard data types
FileInputStream reads from a file
FileOutputStream writes to a file
FilterInputStreamImplements InputStream
FilterOutputStream Implements OutputStream
InputStream Abstract class that describes stream input
ObjectInputStreamfor objects
ObjectOutputStream for objects
OutputStream Abstract class that describes stream output
PipedInputStream Input pipe
PipedOutputStreamOutput pipe
PrintStream Output stream that contains print() and println()
PushbackInputStream supports one-byte "unget," which returns a byte to the input stream
SequenceInputStream a combination of two or more input streams that will be read sequentially, one after the other

Character Stream Classes

Character streams are defined by using two class hierarchies: Reader and Writer.

These abstract classes handle Unicode character streams.

Java has several concrete subclasses of each of these.

Stream ClassMeaning
BufferedReader Buffered input character stream
BufferedWriter Buffered output character stream
CharArrayReader Input stream that reads from a character array
CharArrayWriter Output stream that writes to a character array
FileReader Input stream that reads from a file
FileWriter Output stream that writes to a file
FilterReaderFiltered reader
FilterWriterFiltered writer
InputStreamReader Input stream that translates bytes to characters
LineNumberReaderInput stream that counts lines
OutputStreamWriter Output stream that translates characters to bytes
PipedReader Input pipe
PipedWriter Output pipe
PrintWriter Output stream that contains print() and println()
PushbackReader Input stream that allows characters to be returned to the input stream
Reader Abstract class that describes character stream input
StringReaderInput stream that reads from a string
StringWriterOutput stream that writes to a string
Writer Abstract class that describes character stream output



PreviousNext

Related