codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

49 / 65 topics
43Java Files44Java Create & Write Files45Java Read Files46Java Delete Files47Java I/O Streams48FileInputStream & FileOutputStream49BufferedReader & BufferedWriter
Tutorials/Java Programming/BufferedReader & BufferedWriter
☕Java Programming

BufferedReader & BufferedWriter

Updated 2026-05-12
30 min read

BufferedReader & BufferedWriter

In the world of programming, handling files is a common task. Whether you're reading from or writing to files, it's crucial to do so efficiently to ensure your applications run smoothly. In this tutorial, we'll explore how to use BufferedReader and BufferedWriter for efficient character data handling in Java.

Introduction

When dealing with file I/O operations, especially when reading or writing large amounts of text data, using buffered streams can significantly improve performance. BufferedReader and BufferedWriter are classes that provide buffering capabilities on top of other character-input and character-output streams. This means they read and write data in chunks, reducing the number of I/O operations required.

In this tutorial, we'll dive into how to use these classes effectively, along with some best practices and common pitfalls to avoid.

Core Content

What are BufferedReader and BufferedWriter?

  • BufferedReader: This class reads text from a character-input stream, buffering characters to provide efficient reading of characters, arrays, and lines.

  • BufferedWriter: This class writes text to a character-output stream, buffering characters to provide efficient writing of single characters, arrays, and strings.

Why Use BufferedReader and BufferedWriter?

Using BufferedReader and BufferedWriter is beneficial because:

  1. Efficiency: By reading and writing data in larger chunks (buffers), these classes reduce the number of I/O operations, which can be time-consuming.
  2. Ease of Use: They provide convenient methods for reading lines (readLine()) and writing strings (write()), making file handling more straightforward.

Basic Usage

Let's start with a simple example to demonstrate how to use BufferedReader and BufferedWriter.

Reading from a File with BufferedReader

Java
1import java.io.BufferedReader;
2import java.io.FileReader;
3import java.io.IOException;
4
5public class ReadFileExample {
6 public static void main(String[] args) {
7 try (BufferedReader reader = new BufferedReader(new FileReader("example.txt"))) {
8 String line;
9 while ((line = reader.readLine()) != null) {
10 System.out.println(line);
11 };
12 } catch (IOException e) {
13 e.printStackTrace();
14 }
15 }
16}
Output
Line 1 of the file
Line 2 of the file
Line 3 of the file

Explanation:

  • We create a BufferedReader object that wraps around a FileReader.
  • The readLine() method reads lines from the file until it reaches the end.
  • The try-with-resources statement ensures that the BufferedReader is closed automatically, even if an exception occurs.

Writing to a File with BufferedWriter

Java
1import java.io.BufferedWriter;
2import java.io.FileWriter;
3import java.io.IOException;
4
5public class WriteFileExample {
6 public static void main(String[] args) {
7 try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) {
8 writer.write("Hello, World!");
9 writer.newLine();
10 writer.write("This is a second line.");
11 } catch (IOException e) {
12 e.printStackTrace();
13 }
14 }
15}
Output
Hello, World!
This is a second line.

Explanation:

  • We create a BufferedWriter object that wraps around a FileWriter.
  • The write() method writes strings to the file.
  • The newLine() method inserts a new line character.
  • Again, the try-with-resources statement ensures that the BufferedWriter is closed automatically.

Handling Exceptions

It's important to handle exceptions properly when working with I/O operations. Common exceptions include:

  • FileNotFoundException: Thrown when an attempt to open the file denoted by a specified pathname has failed.
  • IOException: Thrown when an I/O error occurs.

Using try-catch blocks is essential to catch and handle these exceptions gracefully.

Custom Buffer Size

By default, BufferedReader and BufferedWriter use a buffer size of 8192 characters. However, you can specify a custom buffer size if needed:

Java
1import java.io.BufferedReader;
2import java.io.FileReader;
3import java.io.IOException;
4
5public class CustomBufferSizeExample {
6 public static void main(String[] args) {
7 try (BufferedReader reader = new BufferedReader(new FileReader("example.txt"), 1024)) { // Custom buffer size of 1024
8 String line;
9 while ((line = reader.readLine()) != null) {
10 System.out.println(line);
11 }
12 } catch (IOException e) {
13 e.printStackTrace();
14 }
15 }
16}

Explanation:

  • The second parameter in the BufferedReader constructor specifies the buffer size.

Performance Considerations

Using buffered streams can significantly improve performance, especially when dealing with large files. However, it's important to balance buffer size and memory usage. Larger buffers can lead to better performance but may consume more memory.

Practical Example

Let's create a practical example that reads from one file, processes the data, and writes the results to another file using BufferedReader and BufferedWriter.

Java
1import java.io.BufferedReader;
2import java.io.BufferedWriter;
3import java.io.FileReader;
4import java.io.FileWriter;
5import java.io.IOException;
6
7public class FileProcessor {
8 public static void main(String[] args) {
9 try (BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
10 BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) {
11
12 String line;
13 while ((line = reader.readLine()) != null) {
14 // Process the line (e.g., convert to uppercase)
15 String processedLine = line.toUpperCase();
16 writer.write(processedLine);
17 writer.newLine();
18 }
19 } catch (IOException e) {
20 e.printStackTrace();
21 }
22 }
23}

Explanation:

  • This program reads from input.txt, converts each line to uppercase, and writes the results to output.txt.
  • The try-with-resources statement ensures both streams are closed automatically.

Summary

ConceptDescription
BufferedReaderReads text from a character-input stream with buffering for efficiency.
BufferedWriterWrites text to a character-output stream with buffering for efficiency.
Buffer SizeDefault is 8192 characters, but can be customized.
Exception HandlingImportant to handle FileNotFoundException and IOException.
PerformanceBuffered streams improve performance by reducing I/O operations.

What's Next?

In the next tutorial, we'll explore Java Collections, which provide a framework for storing and manipulating groups of objects. Understanding collections is crucial for managing data efficiently in your applications.

Stay tuned for more advanced topics and continue enhancing your Java programming skills!


PreviousFileInputStream & FileOutputStreamNext Java Collections

Recommended Gear

FileInputStream & FileOutputStreamJava Collections