In Java, BufferedReader
and BufferedWriter
are used for efficient reading and writing of text data. Unlike FileReader
or FileWriter
, they use an internal buffer to reduce the number of interactions with the disk, which makes them much faster.
Think of them like this:
Instead of reading or writing one character at a time (which is slow), they read or write a whole chunk of data at once and keep it in memory. This reduces the time it takes to access the file system again and again.
They are part of the java.io
package and extend the Reader
and Writer
classes respectively.
java.lang.Object
↳ java.io.Reader
↳ java.io.BufferedReader
java.lang.Object
↳ java.io.Writer
↳ java.io.BufferedWriter
BufferedReader
extends Reader
BufferedWriter
extends Writer
BufferedReader(Reader reader)
BufferedReader(Reader reader, int bufferSize)
You usually wrap a BufferedReader
around another Reader
like FileReader
.
import java.io.*;
public class BufferedReaderReadLineExample {
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new FileReader("input.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println("Read Line: " + line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Read Line: Hello
Read Line: Java
Read Line: World
readLine()
reads one line at a time and returns null when end of file is reached.- It’s commonly used when working with text data line-by-line.
Reads a single character.
import java.io.*;
public class BufferedReaderSingleChar {
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new FileReader("input.txt"))) {
int ch;
while ((ch = reader.read()) != -1) {
System.out.print((char) ch);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Reads characters into a portion of an array.
import java.io.*;
public class BufferedReaderCharArray {
public static void main(String[] args) {
char[] buffer = new char[50];
try (BufferedReader reader = new BufferedReader(new FileReader("input.txt"))) {
int numChars = reader.read(buffer, 0, buffer.length);
System.out.println("Characters read: " + new String(buffer, 0, numChars));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Characters read: Hello Java
Closes the reader and releases any resources. Automatically handled by try-with-resources.
import java.io.*;
public class BufferedReaderCloseExample {
public static void main(String[] args) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("input.txt"));
System.out.println("First Line: " + reader.readLine());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
System.out.println("Reader closed.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
First Line: Hello
Reader closed.
BufferedWriter(Writer writer)
BufferedWriter(Writer writer, int bufferSize)
Usually wrapped around a FileWriter
.
Writes a string to the buffer.
import java.io.*;
public class BufferedWriterWriteString {
public static void main(String[] args) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) {
writer.write("BufferedWriter writing example");
System.out.println("String written to file.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Writes a platform-specific newline.
import java.io.*;
public class BufferedWriterNewLine {
public static void main(String[] args) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) {
writer.write("Line 1");
writer.newLine(); // Adds a newline
writer.write("Line 2");
System.out.println("Lines written with newLine().");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Lines written with newLine().
File output.txt
content:
import java.io.*;
public class BufferedWriterCharArray {
public static void main(String[] args) {
char[] chars = "Buffered Writer Test".toCharArray();
try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) {
writer.write(chars, 0, chars.length);
System.out.println("Character array written to file.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Character array written to file.
Flushes the buffer, forcing any buffered output to be written.
import java.io.*;
public class BufferedWriterFlushExample {
public static void main(String[] args) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) {
writer.write("Flush this content");
writer.flush();
System.out.println("Data flushed manually.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Closes the writer. Automatically done in try-with-resources.
import java.io.*;
public class BufferedWriterCloseExample {
public static void main(String[] args) {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter("output.txt"));
writer.write("This will be saved on close");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
System.out.println("Writer closed.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
In this blog, we explored BufferedReader
and BufferedWriter
, which improve I/O efficiency using internal buffering. We saw how to read characters and lines, and how to write strings, character arrays, and use new lines and flushing.
In the next blog, we’ll explore StringReader and StringWriter, which help work with strings as streams internally.