3) Input / Output (I/O) Lesson

Java Input Streams

5 min to complete · By Ryan Desmond

A Java InputStream is a class in Java that is the abstract parent class for all types of inputs in I/O. Inversely, the OutputStream is the parent class for all the types of outputs in I/O. These two classes form the foundation of Byte streams.

Why Use Byte Streams?

Programs use Byte streams to read and write data byte by byte, which is equivalent to 8 bits at a time (since 1 byte = 8 bits). All Byte stream classes are child classes of InputStream.java or OutputStream.java.

There are many types of Byte stream classes (see image below). To demonstrate how Byte streams work, you'll focus on the file I/O Byte streams: FileInputStream and FileOutputStream. Other kinds of Byte streams are used in much of the same way; they differ mainly in the way they are constructed. Below is an image showing Java's built-in Byte streams for I/O.

Diagram of InputStream and OutputStream child classes

Using Byte Streams

We'll explore FileInputStream and FileOutputStream by examining an example program named CopyByteByByte (from JavaDoc), which uses Byte streams to copy a file (into a second file), one byte at a time.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

// example class demonstrating simple Byte Stream IO to copy a file from one location to another
public class CopyByteByByte {
  public static void main(String[] args) throws IOException {

    // declare Input and Output Streams outside of 
    // Try statement so they're  accessible in the Finally statement
    FileInputStream in = null;
    FileOutputStream out = null;

    try {

      // initialize them within the Try in case the files 
      // do not exist or cannot be accessed
      in = new FileInputStream("/path/to/some/inputFile.txt");
      out = new FileOutputStream("/path/to/some/outputFile.txt");

      int c;
      // while there is more data to read in the InputStream "in"
      // InputStream's read() method returns -1 
      // when the end of the file is reached
      while ((c = in.read()) != -1) {
        // write the data (byte by byte) to the OutputStream "out"
        out.write(c);
      }
    } catch (IOException exc){
      System.out.println(
        "An error occurred: " + exc.getMessage());
    } finally {
      // close connections to files
      if (in != null) {
        in.close();
      }
      if (out != null) {
        out.close();
      }
    }
  }
}

It is easy to tell that these are Byte streams at work because you can see the words InputStream and OutputStream in FileInputStream and FileOutputStream. Every single Byte Stream has the words InputStream or OutputStream in their class names.

If they were Character Streams, you'd see the words Reader or Writer.

Summary: What is a Java Inputstream?

  • The Inputstream class is the parent class for all inputs in I/O
  • The Outputstream class is the parent class for all outputs in I/O
  • Both classes are the foundation of Byte streams
  • Byte streams ready one byte at a time (1 byte = 8 bits)
  • View example for FileInputStream and FileOutputStream in action