Java's BufferedReader is a fundamental class for character-oriented input. Geared towards efficiency, BufferedReader reads text from a character input stream (characters, strings), loading the data into a buffer before reading. This intelligent design minimizes the number of I/O operations and enhances performance considerably. Let's explore BufferedReader in detail with hands-on examples.
![[2020]-Programming in JAVA - BufferedReader class Practice](https://i.pinimg.com/originals/a5/8c/94/a58c94332308b79b71f1567a4db72e39.jpg)
BufferedReader is typically employed when dealing with large files or external data sources. It's a part of Java's robust I/O (Input/Output) APIs, located within the java.io package. Now, let's dive into its core features and usage.

BufferedReader Initialization
To begin, we initialize BufferedReader. Here are two common methods:

Using FileReader:
```java FileReader fr = new FileReader("filename.txt"); BufferedReader br = new BufferedReader(fr); ```
Using InputStreamReader:

```java Reader r = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(r); ```
Reading Lines
BufferedReader provides several read methods to retrieve data. First, readLine reads a line from the input until the end of the file or stream is reached.
Here's an example reading a file line by line:

```java String line; while ((line = br.readLine()) != null) { System.out.println(line); } ```
Reading Characters
The read() method reads a single character from the input stream. Here's an example that counts characters in a file:
```java int i; while ((i = br.read()) != -1) { System.out.print((char) i); } ```
BufferedReader Enhancements

Java 7 and later versions introduced new features in BufferedReader to make it even more versatile.
Closing Resources with try-with-resources





![10 Examples to Read a Text file in Java [UPDATED]](https://i.pinimg.com/originals/1e/f8/b1/1ef8b1dfc7f4f71f8f3265026e1da828.png)














This feature allows us to automatically close resources, like BufferedReader, at the end of the try block:
```java try (BufferedReader br = new BufferedReader(new FileReader("filename.txt"))) { // Read and process data } ```
Using Method References
BufferedReader's lines() method returns a Stream of the lines in the text. We can process these lines using method references, as illustrated below:
```java br.lines().forEach(System.out::println); ```
With Java's BufferedReader, efficient reading from input sources is a breeze. Mastering this class empowers you to handle I/O like a maestro. Now, embark on your next Java adventure, equipped with this vital input-processing tool!