Friday, July 29, 2022

Byte Streams in Java IO

Byte streams in Java IO are used to perform input and output of 8-bit bytes.

Java InputStream class

All byte stream classes representing an input stream of bytes descend from InputStream. Classes in package representing byte input stream are-

  • InputStream- is an abstract class which is the superclass of all classes representing an input stream of bytes.
  • AudioInputStream- An audio input stream is an input stream with a specified audio format and length. The length is expressed in sample frames, not bytes.
  • ByteArrayInputStream- Used to read from the stream into a byte array buffer.
  • FileInputStream- A FileInputStream obtains input bytes from a file in a file system. FileInputStream is meant for reading streams of raw bytes such as image data.
  • FilterInputStream- A FilterInputStream contains some other input stream, which it uses as its basic source of data, possibly transforming the data along the way or providing additional functionality.
  • BufferedInputStream- This class extends FilterInputStream and adds functionality to buffer to another input stream.
  • DataInputStream- A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. This class extends FilterInputStream.
  • PushbackInputStream- A PushbackInputStream adds functionality to another input stream, namely the ability to "push back" or "unread" one byte.
  • ObjectInputStream- An ObjectInputStream deserializes primitive data and objects previously written using an ObjectOutputStream. Only objects that support the or interface can be read from streams.
  • PipedInputStream- Used to read and write data using two separate threads. A piped input stream should be connected to a piped output stream; the piped input stream then provides whatever data bytes are written to the piped output stream.
  • SequenceInputStream- A SequenceInputStream represents the logical concatenation of other input streams. It starts out with an ordered collection of input streams and reads from the first one until end of file is reached, whereupon it reads from the second one, and so on, until end of file is reached on the last of the contained input streams.

Java OutputStream class

All byte stream classes representing an output stream of bytes descend from OutputStream.

Class in package representing byte output stream are-

  • ByteArrayOutputStream- This class implements an output stream in which the data is written into a byte array.
  • FileOutputStream- A file output stream is an output stream for writing data to a File or to a FileDescriptor.
  • FilterOutputStream- This class is the superclass of all classes that filter output streams.
  • BufferedOutputStream- This class extends FilterOutputStream and provides functionality to buffer output stream.
  • DataOutputStream- A data output stream lets an application write primitive Java data types to an output stream in a portable way. This class extends FilterOutputStream.
  • PrintStream- A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently. This class extends FilterOutputStream.
  • ObjectOutputStream- An ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream. Only objects that support the interface can be written to streams.
  • PipedOutputStream- A piped output stream can be connected to a piped input stream to create a communications pipe. The piped output stream is the sending end of the pipe. Typically, data is written to a PipedOutputStream object by one thread and data is read from the connected PipedInputStream by some other thread.

Java byte stream example

In the Java example FileInputStream and FileOutputStream are used to read and then write bytes to a file one byte at a time.


public class FileByteRW {
  public static void main(String[] args) throws IOException {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
      // Streams
      fis = new FileInputStream("F:\\Temp\\abc.txt");
      fos = new FileOutputStream("F:\\Temp\\write.txt");
      int b;
      while ((b = != -1) {
    } finally {
     // Closing streams
      if (fis != null) {
      if (fos != null) {

When to use byte streams in Java

Byte streams represent low-level I/O that should not be used for normal read write operations. Byte streams should only be used for the most primitive I/O. For character data it is better to use character streams.

That's all for this topic Byte Streams in Java IO. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Advanced Tutorial Page

Related Topics

  1. Java BufferedReader Class With Examples
  2. getPath(), getCanonicalPath() and getAbsolutePath() Methods in Java
  3. How to Read Excel File in Java Using Apache POI
  4. Creating PDF in Java Using iText
  5. How to Read Properties File in Java

You may also like-

  1. Unzip File in Java
  2. Types of JDBC Drivers
  3. JVM Run-Time Data Areas - Java Memory Allocation
  4. Serialization Proxy Pattern in Java
  5. Method Overloading in Java
  6. Access Modifiers in Java - Public, Private, Protected and Default
  7. Spring Job Scheduling Using TaskScheduler And @Scheduled Annotation
  8. Namespace And Variable Scope in Python

No comments:

Post a Comment