Commit 7c49837
committed
feat: demonstrate file reading using FileInputStream (Demo2)
WHAT:
- Implemented a simple program that reads file content byte by byte
using `FileInputStream`.
- Casts each byte to a character and prints to console.
- Wrapped in try-catch to handle `IOException`.
HOW IT WORKS:
1. Open the file with `FileInputStream` pointing to a text file path.
2. Use `fin.read()` in a loop:
- Returns an integer representing the next byte (0–255).
- Returns -1 when EOF is reached, ending the loop.
3. Convert each byte to a `char` and print.
4. Exceptions (e.g., file not found) are handled gracefully.
KEY TAKEAWAYS:
- `FileInputStream` is a byte stream, suitable for both text and binary data.
- Always close streams to avoid resource leaks (try-with-resources is preferred).
- For text files, higher-level classes (`BufferedReader`, `FileReader`)
are more efficient and encoding-aware.
REAL-WORLD USE CASES:
- Reading configuration files or logs at a low level.
- Inspecting binary files (images, PDFs) byte by byte.
- Implementing custom parsers or streaming systems where raw byte access is required.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 2758311 commit 7c49837
File tree
2 files changed
+44
-15
lines changed- Section23JavaIOStreams/DemoCodes
- TXT Files
- src
2 files changed
+44
-15
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
| 1 | + | |
3 | 2 | | |
4 | | - | |
| 3 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | 1 | | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
12 | 8 | | |
13 | | - | |
14 | | - | |
15 | | - | |
| 9 | + | |
| 10 | + | |
16 | 11 | | |
17 | 12 | | |
18 | 13 | | |
19 | | - | |
20 | | - | |
| 14 | + | |
21 | 15 | | |
22 | 16 | | |
23 | 17 | | |
24 | 18 | | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
0 commit comments