Course contentsShow
← Java
Lesson 1980 of 2,87040. File I/O: Character StreamsFree lesson

Reading Characters with read() in BufferedReader

Explore read() methods for reading single characters or character arrays with buffering for better performance.

Reading Characters with read() in BufferedReader

You'll learn how to use read() methods in BufferedReader to efficiently read individual characters or fill character arrays using the internal buffer.

While readLine() is convenient for line-oriented reading, BufferedReader also provides read() methods inherited from Reader — but with an important difference: these methods benefit from internal buffering, making them far more efficient than calling read() directly on a FileReader.

Core read() Methods

BufferedReader offers the same read() methods you've seen in Reader:

  • read() – Returns a single character as an int (0-65535), or -1 if end-of-stream is reached
  • read(char[] buffer) – Fills the provided character array, returns the number of characters actually read
  • read(char[] buffer, int offset, int length) – Fills a portion of the array starting at offset for up to length characters

Why Buffering Matters Here

Without buffering, each read() call would trigger a system call to fetch data from disk. BufferedReader pre-loads chunks of characters into an internal buffer (typically 8KB), so multiple read() calls pull from memory instead — drastically reducing I/O overhead.

Example Pattern

BufferedReader reader = new BufferedReader(new FileReader("data.txt"));
int ch;
while ((ch = reader.read()) != -1) {
    System.out.print((char) ch);
}
reader.close();

Or with a character array for even better performance:

char[] buffer = new char[1024];
int charsRead;
while ((charsRead = reader.read(buffer)) != -1) {
    System.out.print(new String(buffer, 0, charsRead));
}

Key Takeaway: BufferedReader's read() methods provide the same character-reading API as Reader, but with buffered performance — ideal when you need character-level control without line-based parsing.