Table of Contents

Overview of Java I/O Streams

What are I/O streams and why are they needed?

I/O (Input/Output) streams are used to read data from input sources (like files, network connections) and write data to output destinations (like files, network connections). They provide a standard way to handle different types of data sources and destinations, allowing Java programs to interact with the outside world.

I/O streams make it possible for Java programs to interact seamlessly with the external world, handling various types of data efficiently and consistently.

Byte streams and Character streams overview

High-level class structure and key classes

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

public class ByteStreamExample {
    public static void main(String[] args) {
        // Writing bytes to a file
        try (FileOutputStream fos = new FileOutputStream("example.txt")) {
            String data = "Hello, Byte Streams!";
            fos.write(data.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Reading bytes from a file
        try (FileInputStream fis = new FileInputStream("example.txt")) {
            int data;
            while ((data = fis.read()) != -1) {
                System.out.print((char) data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Character Streams

Key Classes: