Commit f9d213a
committed
feat: implement FileExample3 to demonstrate reading files with FileInputStream and try-with-resources
WHAT:
- Added FileExample3 showcasing different ways to read data from a file using FileInputStream.
- Implemented primary logic using a `do-while` loop to read bytes sequentially and cast them to characters.
- Included alternative approaches (commented):
- Using a `while` loop for compact reading.
- Reading all bytes into a byte array (`fis.available()`, `fis.read(b)`) and converting to a String.
WHY:
- Demonstrates how FileInputStream works for reading text data as raw bytes.
- Highlights the immutability of stream operations and different reading strategies.
- Introduces try-with-resources to ensure proper resource management without needing explicit `close()` calls.
HOW:
1. Opened the file `WithResources.txt` with FileInputStream inside try-with-resources.
2. Used `fis.read()` in a loop to read one byte at a time until end-of-file (`-1`).
3. Printed each byte after casting to a character.
4. Added commented examples showing:
- Compact `while` loop approach.
- Reading entire file into a byte array, converting to String, and printing.
BENEFITS:
- Provides clear understanding of low-level file reading in Java.
- Demonstrates different use cases: sequential reading, bulk reading, and character conversion.
- Ensures automatic closing of streams, avoiding resource leaks.
REAL-WORLD APPLICATIONS:
- Reading binary/text files where raw byte processing is needed.
- Useful in file parsers, network protocols, and when working with custom file formats.
- Educational example for beginners learning Java I/O streams.
NOTES:
- For larger files, buffered streams (`BufferedInputStream` or `BufferedReader`) are recommended for efficiency.
- `fis.available()` only provides an estimate of available bytes, not always exact.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 484dd16 commit f9d213a
1 file changed
+57
-10
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
13 | | - | |
| 14 | + | |
| 15 | + | |
14 | 16 | | |
15 | | - | |
16 | | - | |
17 | | - | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
18 | 21 | | |
19 | | - | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
20 | 26 | | |
21 | | - | |
| 27 | + | |
| 28 | + | |
22 | 29 | | |
23 | | - | |
| 30 | + | |
24 | 31 | | |
| 32 | + | |
25 | 33 | | |
26 | 34 | | |
27 | 35 | | |
28 | 36 | | |
29 | 37 | | |
30 | | - | |
| 38 | + | |
| 39 | + | |
31 | 40 | | |
32 | 41 | | |
33 | 42 | | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
0 commit comments