NBKRIST JavaHub

Expert View 💡
In real-world software, handling files efficiently is key. During my experience developing enterprise systems, I used Java File I/O APIs to transfer logs, manage reports, and backup files. Understanding byte, character, and buffered streams helps you choose the right approach for speed, memory, and reliability.

1. FileCopy.java (Byte Stream)

This program demonstrates copying data byte-by-byte using FileInputStream and FileOutputStream.

/** * Demonstrates a basic file-to-file copy using byte streams. * Reads bytes from source and writes to destination file. * Ideal for binary files like images or PDFs. */

import java.io.*;

public class FileCopy {
  public static void main(String[] args) {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    int ch;
    try {
      fis = new FileInputStream("MyView.txt");
      fos = new FileOutputStream("ASectionCSE.txt");
      while ((ch = fis.read()) != -1) fos.write(ch);
      System.out.println("NBKRIST: File copied successfully!");
    } catch (IOException e) {
      System.err.println(e.getMessage());
    } finally {
      try {
        if (fis != null) fis.close();
        if (fos != null) fos.close();
      } catch (IOException e) { System.err.println(e.getMessage()); }
    }
  }
}
  
💡 Tip: Byte streams are preferred for copying any non-text file format.

2. ReaderWriter.java (Character Stream)

Character streams handle text data, automatically managing Unicode encoding.

/** * Demonstrates FileReader and FileWriter for text-based I/O. * Reads characters from 'a.txt' and writes to 'b.txt'. */

import java.io.*;

public class ReaderWriter {
  public static void main(String[] args) {
    try {
      FileReader fr = new FileReader("a.txt");
      FileWriter fw = new FileWriter("b.txt");
      int data;
      while ((data = fr.read()) != -1) fw.write(data);
      fr.close();
      fw.close();
      System.out.println("NBKRIST: Character stream copy done!");
    } catch (IOException e) {
      System.err.println(e.getMessage());
    }
  }
}
  
💡 Tip: Character streams should be used for text to avoid encoding issues.

3. Buffering.java (Buffered Stream)

Buffered streams improve efficiency by reducing physical disk access.

/** * Demonstrates buffered reading/writing. * Uses BufferedReader and PrintWriter for efficient line-based file operations. */

import java.io.*;

public class Buffering {
  public static void main(String[] args) {
    try {
      BufferedReader reader = new BufferedReader(new FileReader("a.txt"));
      PrintWriter writer = new PrintWriter(new FileWriter("kk.txt"), true);
      String line;
      while ((line = reader.readLine()) != null) writer.println(line);
      reader.close();
      writer.close();
      System.out.println("NBKRIST: Buffered stream success!");
    } catch (IOException e) {
      System.err.println(e.getMessage());
    }
  }
}
  
💡 Tip: Always use buffered streams for larger files to enhance performance.

Practice Questions 🧠

1. Which classes are used for byte stream operations?
2. How do character streams differ from byte streams?
3. Why use buffered streams?

Key Summary 📘