I/O streams in Java

There are two kinds of I/O streams:

  • byte - java.io.InputStream, java.io.OutputStream
  • symbolic - java.io.Reader, java.io.Writer

I/O stream classes are placed in java.io, java.nio packages. To work with streams of compressed data, classes from the java.util.zip package are used.

Subclasses of the InputStream class

  • InputStream is an abstract class that describes an input stream.
  • BufferedInputStream is the buffered input stream.
  • ByteArrayInputStream allows you to use an in-memory buffer (byte array) as a data source for an input stream.
  • DataInputStream is an input stream for byte data, including methods for reading standard Java data types.
  • FileInputStream is an input stream for reading information from a file.
  • FilterInputStream is an abstract class that provides an interface for add-in classes that add useful properties to existing streams.
  • ObjectInputStream is an input stream for objects.
  • StringBufferInputStream turns a String into an InputStream.
  • PipedInputStream implements the concept of an input channel.
  • PushbackInputStream is a kind of buffering that provides reading a byte and then returning it to the stream, allowing you to "look" into the input stream and see what will come from there the next moment without extracting information.
  • SequenceInputStream is used to merge two or more InputStreams into a single one.

SequenceInputStream

The SequenceInputStream class lets you merge multiple instances of the InputStream class together. The constructor takes either a pair of InputStream objects or an Enumeration interface as an argument.

At runtime, the class executes read requests from the first InputStream to the end, and then switches to the second. When using the interface, work will continue on all objects of the InputStream class. When the end is reached, the associated stream is closed. Closing a stream created by an object of the SequenceInputStream class closes all open streams.

Class that allows to read data from an input byte stream in the format of primitive data types

The DataInputStream class represents an input stream and is designed to write data of primitive types such as int, double, etc. Each primitive type has its own method for reading:

  • boolean readBoolean(): reads a single-byte boolean value from the stream
  • byte readByte(): reads 1 byte from the stream
  • char readChar(): reads a char value from the stream
  • double readDouble(): reads an 8-byte double value from the stream
  • float readFloat(): reads a 4-byte float value from the stream
  • int readInt(): reads an integer int value from the stream
  • long readLong(): reads a long value from the stream
  • short readShort(): reads a short value
  • String readUTF(): reads a UTF-8 encoded string from the stream

Read also:


Comments

Popular posts from this blog

Methods for reading XML in Java

XML, well-formed XML and valid XML

ArrayList and LinkedList in Java, memory usage and speed