Back to Study Material Java

File Handling in Java

File handling in Java involves reading data from files on disk and writing data back to them. The java.io package provides the core classes for this, organ...

File Handling in Java

Reading and Writing Files with java.io

File handling in Java involves reading data from files on disk and writing data back to them. The java.io package provides the core classes for this, organized around two hierarchies: character streams (Reader and Writer) for text data and byte streams (InputStream and OutputStream) for binary data. Choosing the right stream type depends on what you are working with: text files use character streams to handle encoding properly, while images, audio, or serialized objects use byte streams.

The File Class java.io.File represents a file or directory path in the filesystem. It does not open the file; it just holds path information and provides methods to query it: exists(), isDirectory(), length(), list(), mkdir(), and delete(). For actual reading and writing, you pair a File with a stream class. Since Java 7, the Paths and Files classes in java.nio.file provide a cleaner, more powerful alternative to java.io.File.

Reading Text Files FileReader wraps a File and reads one character at a time, which is inefficient on its own. Wrapping it in a BufferedReader adds an internal buffer so Java reads a large chunk from disk at once and feeds characters from the buffer. The readLine() method of BufferedReader reads a full line as a String, returning null at end-of-file. Modern Java (8+) allows you to read all lines into a List with Files.readAllLines(path) or stream them lazily with Files.lines(path).

Writing Text Files FileWriter writes characters or strings to a file. Wrapping it in a BufferedWriter adds buffering and the newLine() convenience method, which writes the platform-correct line separator. When constructing a FileWriter, a second boolean argument controls append mode: new FileWriter("log.txt", true) appends to the existing file instead of overwriting it. Always flush and close the writer when done; unflushed data may stay in the buffer and never reach disk.

Code Example: import java.io.*; import java.nio.file.*;

// Modern approach - write Files.writeString(Path.of("output.txt"), "Hello, Java Files!");

// Modern approach - read String content = Files.readString(Path.of("output.txt")); System.out.println(content); // Hello, Java Files!

// Traditional approach - read line by line try (BufferedReader br = new BufferedReader(new FileReader("output.txt"))) { String line; while ((line = br.readLine()) != null) System.out.println(line); }

Interview Tip: Interviewers often ask what happens if you forget to close a file stream. The operating system allows only a finite number of open file handles per process. Forgetting to close streams eventually causes a "too many open files" error. Always use try-with-resources to guarantee streams are closed. In the question context, also mention that unwritten data in a BufferedWriter's buffer is lost if the writer is not properly closed.