Java I/O Streams

Understanding input and output operations in Java

📁 What are I/O Streams?

Java I/O Streams are used to read data from input sources and write data to output destinations. They provide a consistent way to handle data flow in Java applications efficiently.


// Simple I/O Stream example
import java.io.*;

public class StreamExample {
    public static void main(String[] args) {
        System.out.println("Hello, I/O Streams!");
    }
}
                                    

Output:

Hello, I/O Streams!

Types of I/O Streams

📥

Input Streams

Read data from sources

InputStream input = new FileInputStream("file.txt");
📤

Output Streams

Write data to destinations

OutputStream output = new FileOutputStream("file.txt");
🔤

Character Streams

Handle text data efficiently

Reader reader = new FileReader("text.txt");
🔢

Byte Streams

Handle binary data

byte[] data = {65, 66, 67};

🔹 Basic Stream Operations

Common operations you'll perform with I/O streams:

import java.io.*;

public class BasicStreamOps {
    public static void main(String[] args) {
        try {
            // Reading from a file
            FileInputStream fis = new FileInputStream("input.txt");
            int data = fis.read();
            System.out.println("First byte: " + data);
            fis.close();
            
            // Writing to a file
            FileOutputStream fos = new FileOutputStream("output.txt");
            fos.write(65); // Writes 'A'
            fos.close();
            
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

Output:

First byte: 72

File 'output.txt' created with content 'A'

🔹 Stream Hierarchy

Understanding the Java I/O class hierarchy:

Byte Streams:

  • InputStream - Abstract class for reading bytes
  • OutputStream - Abstract class for writing bytes
  • FileInputStream/FileOutputStream - File operations

Character Streams:

  • Reader - Abstract class for reading characters
  • Writer - Abstract class for writing characters
  • BufferedReader/BufferedWriter - Buffered operations

🔹 Exception Handling

Always handle exceptions when working with I/O streams:

import java.io.*;

public class SafeStreamHandling {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("data.txt");
            int content = fis.read();
            System.out.println("Content: " + (char)content);
        } catch (FileNotFoundException e) {
            System.out.println("File not found!");
        } catch (IOException e) {
            System.out.println("Error reading file!");
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    System.out.println("Error closing file!");
                }
            }
        }
    }
}

🧠 Test Your Knowledge

Which class is used to read bytes from a file?