Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Lesson 13: File I/O — Streams, Readers/Writers, Serialization & NIO

Lesson 14 of 18 in the free Programming in Java notes on Siksha Sarovar, written by Rohit Jangra.

13.1 The File Class — Metadata, Not Contents

java.io.File represents a path (file or directory) and manipulates metadata only — it never reads contents:

File f = new File("notes.txt");
f.exists(); f.length(); f.isDirectory(); f.getAbsolutePath();
f.createNewFile(); f.delete(); f.renameTo(dest);
new File("reports/2025").mkdirs();          // create nested dirs
for (String name : new File(".").list()) System.out.println(name);

13.2 Byte Streams vs Character Streams

AspectByte streamsCharacter streams
Base classesInputStream / OutputStreamReader / Writer
Unit8-bit bytes16-bit chars (charset-decoded)
ForImages, audio, PDFs, ZIPs — any binaryText (handles UTF-8 etc. correctly)
Key concrete typesFileInputStream, FileOutputStreamFileReader, FileWriter
Buffered wrappersBufferedInputStream/OutputStreamBufferedReader/Writer
Bridge classesInputStreamReader / OutputStreamWriter

Java I/O uses the Decorator pattern: wrap streams inside streams — new BufferedReader(new InputStreamReader(System.in)) was the standard console-input idiom before Scanner. Reading text through byte streams corrupts multi-byte UTF-8 characters — the classic "why character streams exist" answer.

13.3 Buffered Text I/O — The Workhorse Pattern

try (BufferedReader br = new BufferedReader(new FileReader("in.txt"));
     BufferedWriter bw = new BufferedWriter(new FileWriter("out.txt", true))) { // true = append
    String line;
    while ((line = br.readLine()) != null) {   // null signals EOF
        bw.write(line.toUpperCase());
        bw.newLine();                          // platform-correct line ending
    }
}   // both closed automatically, reverse order

Buffering batches disk hits (default 8 KB buffer) — orders of magnitude faster than unbuffered per-char reads. readLine() returns null at EOF (not an exception). flush() pushes buffered bytes out; close() flushes then releases the OS handle. Also know PrintWriter (printf-style convenience, never throws IOException — check checkError()).

13.4 Serialization — Objects to Bytes

Serialization converts an object graph into a byte stream (file, socket, cache); deserialization rebuilds it.

class Account implements Serializable {                 // marker interface!
    private static final long serialVersionUID = 1L;    // version stamp
    String owner;
    transient String password;                          // excluded from the stream
    static int branchCode = 7;                          // static: never serialized
}
try (var oos = new ObjectOutputStream(new FileOutputStream("acc.ser"))) {
    oos.writeObject(acct);
}
try (var ois = new ObjectInputStream(new FileInputStream("acc.ser"))) {
    Account back = (Account) ois.readObject();          // cast + ClassNotFoundException
}   // back.password == null, branchCode from the CLASS, not the stream

Must-know rules: the class implements Serializable or NotSerializableException is thrown; transient fields deserialize to defaults; static fields belong to the class, not the object, so they are skipped; every non-transient field's type must itself be serializable; serialVersionUID guards version compatibility — omit it and any recompile can make old files unreadable (InvalidClassException); parent-class state is serialized only if the parent is also Serializable, otherwise its no-arg constructor runs on read.

13.5 NIO.2 Glance — Path & Files (Java 7+)

Path p = Paths.get("data", "log.txt");
Files.writeString(p, "hello");                 // one-liners (Java 11)
String s = Files.readString(p);
List<String> lines = Files.readAllLines(p);
Files.copy(p, Paths.get("backup.txt"), StandardCopyOption.REPLACE_EXISTING);
try (Stream<String> st = Files.lines(p)) { st.filter(l -> l.contains("ERROR")).forEach(System.out::println); }

NIO adds buffers + channels (non-blocking I/O), DirectoryStream, WatchService, and rich metadata — for this course, know Path/Files one-liners and that NIO is the modern replacement for most File juggling.

🎯 Exam Focus

  1. Differentiate byte streams and character streams, naming the four abstract base classes and one concrete class each.
  2. Explain the roles of BufferedReader and BufferedWriter. Why is buffering faster? Write a program to copy a text file line by line.
  3. What is serialization? Explain Serializable, serialVersionUID, and the effect of transient and static on serialized state.
  4. Write a program to serialize a Student object to student.ser and read it back, printing the restored state.
  5. How does try-with-resources improve I/O code? What happens to the file handle if an exception occurs mid-read?
  6. Give NIO.2 equivalents (Path/Files) for reading all lines and copying a file.