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](https://i.pinimg.com/originals/a5/8c/94/a58c94332308b79b71f1567a4db72e39.jpg)
This article will provide an in-depth understanding of these two classes and will illustrate their use with examples.

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

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]](https://i.pinimg.com/originals/1e/f8/b1/1ef8b1dfc7f4f71f8f3265026e1da828.png)
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

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

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.




















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.