In Java, BufferedReader and BufferedWriter are classes designed to handle character-based input and output operations efficiently. These classes are used for reading and writing text data (characters) from/to files or other input/output sources like network streams.
They work similarly to BufferedInputStream and BufferedOutputStream, but while the latter are used for byte-based data, BufferedReader and BufferedWriter are used for character-based data, making them ideal for handling text files.
BufferedReader
is a class in Java that wraps around other Reader classes (like FileReader
) to provide efficient reading of characters, arrays, and lines of text. It reads large chunks of data into a buffer, and then the program reads data from the buffer rather than directly from the underlying source, improving performance.
BufferedReader
reads large chunks of data from the file into an internal buffer. Subsequent reads are taken from the buffer, reducing the number of I/O operations and improving performance.Constructors of BufferedReader
BufferedReader(Reader in)
BufferedReader(Reader in, int size) // in: reader object (like FileReader), size: buffer size
FileReader
, from which data will be read.read()
: Reads a single character.read(char[] cbuf, int off, int len)
: Reads characters into a portion of an array.readLine()
: Reads a line of text.close()
: Closes the reader and releases system resources.import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReaderExample {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("example.txt"))) {
String line;
while ((line = br.readLine()) != null) { // Reads one line at a time
System.out.println(line); // Print the line to the console
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Explanation:
BufferedReader
wraps the FileReader
to add buffering.br.readLine()
: Reads one line of text at a time from the file example.txt
, making it more efficient for text data.BufferedWriter
is a class in Java that wraps around other Writer classes (like FileWriter
) to provide efficient writing of characters, arrays, and strings. It stores characters in a buffer before writing them to the file or output stream, reducing the number of I/O operations by writing large chunks of data at once.
BufferedWriter
accumulates characters in a buffer and only writes the data when the buffer is full or when the flush()
method is called, reducing the number of writes to the underlying system.Constructors of BufferedWriter
BufferedWriter(Writer out)
BufferedWriter(Writer out, int size) // out: writer object (like FileWriter), size: buffer size
FileWriter
, to which data will be written.write(String s)
: Writes a string to the output.write(char[] cbuf, int off, int len)
: Writes characters from a portion of an array.newLine()
: Writes a newline character to the output.flush()
: Flushes the output stream, writing any buffered data to the file.close()
: Closes the writer and releases system resources.xxxxxxxxxx
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterExample {
public static void main(String[] args) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"))) {
bw.write("Hello, BufferedWriter!"); // Writing string to buffer
bw.newLine(); // Writing a newline character
bw.write("Writing with efficiency.");
bw.flush(); // Ensure all data is written to the file
} catch (IOException e) {
e.printStackTrace();
}
}
}
Explanation:
BufferedWriter
wraps the FileWriter
to add buffering.bw.write()
: Writes the string "Hello, BufferedWriter!" to the buffer.bw.newLine()
: Writes a new line to the file.bw.flush()
: Ensures that all buffered data is written to the file before closing the stream.output.txt
contains the text written by the program.Here’s an example of how to use BufferedReader
and BufferedWriter
together to copy a text file efficiently.
xxxxxxxxxx
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedCopyFileExample {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("sourceFile.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("destinationFile.txt"))) {
String line;
while ((line = br.readLine()) != null) { // Read line by line
bw.write(line); // Write each line to the new file
bw.newLine(); // Add a new line to the output file
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Explanation:
sourceFile.txt
using readLine()
.destinationFile.txt
using write()
, followed by newLine()
to preserve the file’s structure.