Java BufferedReader BufferedWriter Example Tutorial With Code Snippets

Robert Jul 13, 2026

Java provides several classes for file handling, among which BufferedReader and BufferedWriter are extensively used for efficient reading and writing of text files. BufferedReader reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters. BufferedWriter, on the other hand, writes text to a character-output stream, buffering characters so as to provide for the efficient writing of characters.

[2020]-Programming in JAVA - BufferedReader class Practice
[2020]-Programming in JAVA - BufferedReader class Practice

This article will provide an in-depth understanding of these two classes and will illustrate their use with examples.

BufferedByteStream - Java | Geekboots
BufferedByteStream - Java | Geekboots

Java BufferedReader

The BufferedReader class in Java simplifies the process of reading small chunks of data.

☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀

It is often used in conjunction with a legacy API that returns data one line at a time, for example, a java.io.Reader or an InputStreamReader.

Creating a BufferedReader

10 Examples to Read a Text file in Java [UPDATED]
10 Examples to Read a Text file in Java [UPDATED]

The BufferedReader class is created using the BufferedReader(InputStreamReader in, int sz) constructor, where the input stream is wrapped in a BufferedReader where the buffer size is specified. Here's a simple example:

```java import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String sentence = br.readLine(); System.out.println("You entered : " + sentence); } } ```

In this example, BufferedReader is used to read a single line of text from the user.

Reading Larger Files with BufferedReader

Multithreading in Java Explained | Thread Life Cycle & Creation Methods
Multithreading in Java Explained | Thread Life Cycle & Creation Methods

BufferedReader can also be used for reading larger files line by line. Here's an example:

```java import java.io.*; public class Main { public static void main(String[] args) { try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } } ```

This reads the entire file line by line and prints it to the console.

Java BufferedWriter

Java Notes
Java Notes

BufferedWriter is used for writing data to files efficiently. It writes data by batching together writes for the underlying character stream, which can significantly improve performance on many IList.

The BufferedWriter class wraps another writer so as to provide for tamper proof, but comes with different, more convenient methods.

Polymorphism in Java Explained | Method Overloading & Overriding Easy Notes
Polymorphism in Java Explained | Method Overloading & Overriding Easy Notes
Java Data Types Made Easy for Beginners
Java Data Types Made Easy for Beginners
The OOP'S Concept in JAVA🤔💯✅👇
The OOP'S Concept in JAVA🤔💯✅👇
☕ Java Operators Cheat Sheet | Master Java Operators Easily 🚀
☕ Java Operators Cheat Sheet | Master Java Operators Easily 🚀
Free Java Programming Book
Free Java Programming Book
Java Switch Example
Java Switch Example
Encapsulation in Java Explained Simply | OOP Concept with Example 💻✨
Encapsulation in Java Explained Simply | OOP Concept with Example 💻✨
FileWriter in Java (with Example) - Scientech Easy
FileWriter in Java (with Example) - Scientech Easy
javascript copyright footer
javascript copyright footer
Java Type Promotion, Type Casting & Conversion Notes
Java Type Promotion, Type Casting & Conversion Notes
Programming Books Archives - Techprofree
Programming Books Archives - Techprofree
Is learning Java worth in 2019?
Is learning Java worth in 2019?
Abstraction in Java | Abstract Class vs Interface Explained Simply
Abstraction in Java | Abstract Class vs Interface Explained Simply
Java Variables Explained in 60 Seconds | Java Programming Basics
Java Variables Explained in 60 Seconds | Java Programming Basics
a poster describing how to use the data flow chart for an organization's workflow
a poster describing how to use the data flow chart for an organization's workflow
Uses of Java
Uses of Java
JavaScript Basics You MUST Know
JavaScript Basics You MUST Know
an image of a text description for a web page
an image of a text description for a web page
Master Java 🚀 Advanced Cheat Sheet for Real Developers
Master Java 🚀 Advanced Cheat Sheet for Real Developers
List of Java Operators with Examples | Complete Guide for Beginners
List of Java Operators with Examples | Complete Guide for Beginners

Writing to a File with BufferedWriter

Here's how you can write to a file using BufferedWriter:

```java import java.io.*; public class Main { public static void main(String[] args) { try (BufferedWriter bw = new BufferedWriter(new FileWriter("file.txt"))) { bw.write("Test BufferedWriter"); bw.newLine(); bw.write("This is another test line"); bw.flush(); } catch (IOException e) { e.printStackTrace(); } } } ```

This writes two lines to a file "file.txt", the first being "Test BufferedWriter" and the second being "This is another test line".

Using BufferedWriter with PrintWriter

The PrintWriter class extends Writer and provides a higher level API for printing to files. It is often used in conjunction with BufferedWriter to add even more functionality. Here's an example:

```java import java.io.*; public class Main { public static void main(String[] args) { try (PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("file.txt")))) { pw.println("Test PrintWriter and BufferedWriter"); pw.println("This is another test line"); } catch (IOException e) { e.printStackTrace(); } } } ```

This code does the same thing as the previous example, but uses the PrintWriter API for easy printing.

In conclusion, Java BufferedReader and BufferedWriter classes are fundamental to efficient file handling. They provide a convenient and effective way to read from and write to files. Whether you're reading a single line of data or processing a large file, these classes can help make the task easier and faster.