Skip to content

Commit 4c06769

Browse files
committed
feat: add BufferReaderExample to demonstrate efficient text reading with BufferedReader
WHAT: - Implemented `BufferReaderExample` showcasing the use of `BufferedReader` with `FileReader`. - Demonstrated: - Reading characters one by one. - Using `mark()` and `reset()` to bookmark and return to a specific position in the stream. - Using `readLine()` to read an entire line of text efficiently. - Closed resources properly after use. WHY: - `BufferedReader` provides efficient reading of characters, arrays, and lines compared to raw `FileReader`. - `mark()` and `reset()` allow controlled navigation inside a stream, useful in parsers or text processors. - Demonstrates the difference between reading characters vs reading lines. HOW: 1. Opened a text file using `FileReader`. 2. Wrapped it with `BufferedReader` to enable buffered reading. 3. Read a few characters sequentially. 4. Marked the stream, read ahead, and reset back to the mark. 5. Continued reading, then retrieved the rest of the line using `readLine()`. 6. Closed the reader to release system resources. REAL-WORLD USE CASES: - Parsing configuration files or logs line by line. - Efficiently reading large text files without loading everything into memory. - Useful in compilers/interpreters where token lookahead requires `mark()` and `reset()`. - Forms the backbone of text-based file I/O in Java. NOTES: - Always pair `mark()` with `reset()`, and ensure `markSupported()` is true before calling. - Unlike `FileInputStream`, `BufferedReader` is character-based and encoding-aware (good for text files). - Best practice: use try-with-resources to auto-close streams. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent b4be296 commit 4c06769

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

Section23JavaIOStreams/src/BufferReaderExample.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
public class BufferReaderExample {
55
public static void main(String[] args)throws Exception {
6-
// Creating a FileReader to read the file character by character
7-
FileReader fis = new FileReader("C://Users//somes//Downloads//JAVA SE//Section23JavaIOStreams//src//MyJAVA//BufferTest.txt");
6+
// Creating a FileReader to read the file character by character.
7+
FileReader fis = new FileReader("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/src/MyJAVA/BufferTest.txt");
8+
89
BufferedReader bis = new BufferedReader(fis);
9-
// BufferedReader is used to read text efficiently from a character-based input stream
10+
// BufferedReader is used to read text efficiently from a character-based input stream.
1011

1112
System.out.print(( char ) bis.read());
1213
System.out.print(( char ) bis.read());
@@ -20,9 +21,9 @@ public static void main(String[] args)throws Exception {
2021
System.out.print(( char ) bis.read());
2122
System.out.print(( char ) bis.read());
2223

23-
// Reading and printing the remaining line from the file
24+
// Reading and printing the remaining line from the file.
2425
System.out.println("String "+bis.readLine());
2526

2627
bis.close();
2728
}
28-
}
29+
}

0 commit comments

Comments
 (0)