NBKRIST JavaHub

Expert View πŸ’‘
During my experience as a software engineer, I realized how mastering Java’s I/O API is essential for any real-time application. From banking transactions to industrial automation, efficient input/output handling ensures reliability and scalability. Once students understand this concept, they can confidently build robust, file-driven and network-based applications.

Introduction

The Java I/O API is a powerful system for handling the input and output of data in applications. It revolves around the concept of streams, representing a flow of data between a source and destination.

Java I/O Stream Architecture
πŸ’‘ Tip: Think of streams as pipelines through which Java moves data from sources like files or networks to destinations efficiently.

Standard I/O Streams

Example:

public class StandardIO {
    public static void main(String[] args) {
        System.out.println("NBKRIST: Standard Output Message");
        System.err.println("NBKRIST: Error Message Example");
    }
}
        
πŸ’‘ Tip: Use System.err for logging critical errors to keep console output clean.

Byte Streams

Byte streams handle 8-bit raw data, ideal for binary files such as images or audio. They use InputStream and OutputStream classes.


import java.io.*;

public class ByteStreamExample {
    public static void main(String[] args) {
        try (FileInputStream in = new FileInputStream("input.dat");
             FileOutputStream out = new FileOutputStream("output.dat")) {
            int b;
            while ((b = in.read()) != -1) {
                out.write(b);
            }
            System.out.println("NBKRIST: File copied successfully using byte streams.");
        } catch (IOException e) {
            System.err.println("I/O Error: " + e.getMessage());
        }
    }
}
        
NBKRIST: File copied successfully using byte streams.
πŸ’‘ Tip: Use byte streams for any file containing binary content such as PDFs, images, or audio.

Character Streams

Character streams process 16-bit Unicode characters. They are best for text files, automatically managing character encoding.

Character Streams in Java

import java.io.*;

public class CharacterStreamExample {
    public static void main(String[] args) {
        try (FileReader fr = new FileReader("input.txt");
             FileWriter fw = new FileWriter("output.txt")) {
            int c;
            while ((c = fr.read()) != -1) {
                fw.write(c);
            }
            System.out.println("NBKRIST: Character Stream Success!");
        } catch (IOException e) {
            System.err.println("I/O Error: " + e.getMessage());
        }
    }
}
        
NBKRIST: Character Stream Success!
πŸ’‘ Tip: Character streams automatically handle text encoding, avoiding data loss in multilingual applications.

Scanner Class

The Scanner class (from java.util) reads input easily from the keyboard or text files.


import java.util.Scanner;

public class ScannerExample {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your name: ");
        String name = sc.nextLine();
        System.out.println("Welcome to NBKRIST, " + name + "!");
        sc.close();
    }
}
        
Enter your name: Hari
Welcome to NBKRIST, Hari!
πŸ’‘ Tip: Scanner is excellent for small input tasks but not optimal for large file processing.

Files API (java.nio.file.Files)

Introduced in Java 7, the java.nio.file.Files class provides static methods for file and directory operations β€” modern, efficient, and easy to use.


import java.nio.file.*;
import java.io.IOException;

public class FilesExample {
    public static void main(String[] args) {
        String content = "NBKRIST - JavaHub Modern File I/O Example";
        Path filePath = Path.of("nio_output.txt");
        try {
            Files.writeString(filePath, content);
            System.out.println("Content written successfully using Files API!");
        } catch (IOException e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}
        
Content written successfully using Files API!
πŸ’‘ Tip: Prefer the Files API for modern Java development β€” it simplifies file reading/writing with fewer lines of code.

Practice Questions 🧠

1. What are the three standard I/O streams in Java?
2. What’s the key difference between byte and character streams?
3. Why are Scanner and Files API important in Java?

Key Summary πŸ“˜

⬆
⬇