Java I/O Byte Streams

In this section we will discussed the I/O Byte Streams.

Java I/O Byte Streams

In this section we will discussed the I/O Byte Streams.

Java I/O Byte Streams

Java I/O Byte Streams

In this section we will discussed the I/O Byte Streams.

This Stream handles the 8-bit binary input/output of data. Byte streams are used where the program needs to work with the raw binary data. In Java to handle I/O raw binary data the byte stream classes are defined. For all of the byte stream classes InputStream and OutputStream are the root classes.

An image given below demonstrate the class hierarchy of Byte Stream classes.

Classes of byte stream

InputStream : An abstract super class of all the classes which represents the input data as byte (8-bit).

Methods of InputStream

  • available() : returns the number of readable bytes from the input stream.

    public int available() throws IOException
     
  • close() : This method is used to close the input stream and the resource associated to the stream is get released.

    public void close() throws IOException
     
  • mark(int readLimit) : This method is used to mark the current position in the input stream.

    public void mark(int readlimit)
     
  • markSupported() : This method is used to specify that whether the input stream supports the mark and reset methods.

    public boolean markSupported()
     
  • read() : This method is used to read next byte of data of input stream.

    public abstract int read() throws IOException
     
  • read(byte[] b) : This method is used to read the byte from the input stream and kept them to the buffer array.

    public int read(byte[] b) throws IOException
     
  • read(byte[] b, int off, int len) : This method is used to read bytes from the input stream upto the len bytes.

    public int read(byte[] b, int off, int len) throws IOException
     
  • reset() : This method is used to repositioned the stream where the mark method was marked the current position in the input streeam.

    public void reset() throws IOException
     
  • skip(long n) : This method is used to skipped over and discarded the data of n bytes from the input stream.

    public long skip(long n) throws IOException
     

Some subclasses of the InputStream class are as follows :

Byte Stream Classes Description
FileInputStream FileInputStream takes input bytes from a file.
ByteArrayInputStream A ByteArrayInputStream can retain bytes read from the input stream using its internal buffer
SequenceInputStream The input streams can be logically concatenated using SequenceInputStream.
StringBufferInputStream The string's content supplies bytes read to an input stream which is created by an application.
The StringBufferInputStream class allows an application to do this.
FilterInputStream It has some additional input stream used as basic data source, these input streams can
provide transformation of data and extra functionality.
DataInputStream It allows an application to read java's data type using an input stream which is independent of machine.
BufferedInputStream It provides additional functionality to the input stream such as capability to buffer. It also supports reset and mark methods.
PipedInputStream The data byte written on piped output stream is provided by PipedInputStream. But before that it must connect to the piped output stream.
PushbackInputStream To sum up the ability to "push back" or "unread" one byte to other input stream, a PushbackInputStream is used.

OutputStream : An abstract super class of all the classes which represents the output data as byte (8-bit).

Methods of OutputStream

  • close() : This method is used to close the output stream and the resource associated to the stream is get released.

    public void close() throws IOException
     
  • flush() : This method is used to flush the output stream and if there is any unwritten output bytes in the buffer forced to write out them.

    public void flush() throws IOException
     
  • write(byte[] b) : This method is used to write the b.length bytes of a specified byte array to the output stream.

    public void write(byte[] b) throws IOException
     
  • write(byte[] b, int off, int len) : This method is used to write the specified byte (len) of a specified byte array started from the specified offset (off) to the output stream.

    public void write(byte[] b, int off, int len) throws IOException
     
  • write(int b) : This method is used to write the specified byte into the output stream.

    public abstract void write(int b) throws IOException
     

Some subclasses of OutputStream class are as follows :

Byte Stream Classes Description
FileOutputStream FileOutputStream is used write data to a File or to a FileDescriptor.
ByteArrayOutputStream ByteArrayOutputUsingStream provides the facility to write data into a byte array using the output stream.
FilterOutputStream All the classes that filter output streams is the subclasses of the FilterOutputStream superclass.
DataOutputStream It allows an application to write java's data type(primitive) to an output stream.
BufferedOutputStream The BufferedOutputStream provides the facility to contains the byte into the buffered output stream.
PrintStream It provide ability to the other output stream to print several data value's representation.
PipedOutputStream For creating communication pipe, piped output stream should be connected to piped input stream.

For detail description please visit the java doc of java.io package