Commit 4c06769
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
1 file changed
+6
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | | - | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
8 | 9 | | |
9 | | - | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
23 | | - | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
28 | | - | |
| 29 | + | |
0 commit comments