I/O Streams In Java

Java's Input/Output (I/O) streams are an essential concept for handling data input and output in applications. Let's start with the basics and then gradually move to more advanced concepts.

What is a Stream?

A stream is a sequence of data elements made available over time. In Java, streams are used to read data from input devices or write data to output devices.

Key Points:

  • Streams provide a way to input data from devices like the keyboard, network, files, etc.
  • Streams also allow output of data to devices like monitors, printers, network, etc.
  • Java uses the java.io package to represent these streams through predefined classes.

Types of Streams in Java

Java has two primary categories of streams:

  • Byte-Oriented Streams: These handle input and output of raw binary data (bytes).
  • Character-Oriented Streams: These handle input and output of characters, and are generally used for text data.
  1. Byte-Oriented Streams

    InputStream: Reads binary data (byte-by-byte) from a source (e.g., file, keyboard).

    OutputStream: Writes binary data (byte-by-byte) to a destination (e.g., file, monitor).

  2. Character-Oriented Streams

    Reader: Reads text (character-by-character) from a source.

    Writer: Writes text (character-by-character) to a destination.

How I/O Streams Work?

Streams are like a pipeline through which data flows between the input devices and output devices. Data is read from the input stream, processed in the application, and then written to the output stream.

Flow of I/O Streams:

  1. InputStream: Data enters the application from sources like the keyboard, mouse, or files.
  2. Reader: Converts the data from the binary (byte) stream to characters.
  3. Application Logic: The program processes the data.
  4. Writer: Converts characters back to bytes (if needed).
  5. OutputStream: Data is sent to output devices like the monitor, printer, or file.

Byte-Oriented Streams Example

  • InputStream Example (Reading from a File)
  • OutputStream Example (Writing to a File)
OutputStream Example (Writing to a File)

Explanation:

  • FileOutputStream: Writes bytes to a file.
  • fos.write(): Converts the string to a byte array and writes it to the file.
InputStream Example (Reading from a File)

Explanation:

  • FileInputStream: This class is used to read bytes from a file.
  • fis.read(): Reads a single byte at a time from the file. If there’s no more data, it returns -1.
  • System.out.print((char) data): Converts the byte to a character and prints it.

Character-Oriented Streams Example

  • Reader Example (Reading Characters from a File)
  • Writer Example (Writing Characters to a File)
Reader Example (Reading Characters from a File)

Explanation:

  • FileReader: Reads characters from a file.
  • fr.read(): Reads one character at a time and converts it to a character using (char).
Writer Example (Writing Characters to a File)

Explanation:

  • FileWriter: Writes characters to a file.
  • fw.write(): Writes a string to the file, character-by-character.