Skip to content

Commit f9d213a

Browse files
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

File tree

1 file changed

+57
-10
lines changed

1 file changed

+57
-10
lines changed

Section23JavaIOStreams/src/FileExample3.java

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,79 @@
22

33
public class FileExample3 {
44
public static void main(String[] args)throws Exception {
5-
try(FileInputStream fis = new FileInputStream("C://Users//somes//Downloads//JAVA SE//Section23JavaIOStreams//src//MyJAVA/WithResources.txt");)
5+
try(FileInputStream fis = new FileInputStream("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/src/MyJAVA/WithResources.txt");)
66
{
77
//Reading bytes from file and letter by letter.
88
int x;
9-
/* other ways to do. without do while. we can use simple while loop
9+
10+
/* other ways to do. without do while. we can use a simple while loop.
1011
while((x=fis.read())!=-1)
1112
{
1213
System.out.println((char)x);
13-
}*/
14+
}
15+
*/
1416

15-
do{
16-
x=fis.read();
17-
if(x!=-1)
17+
do
18+
{
19+
x = fis.read();
20+
if (x!=-1)
1821
System.out.print((char)x);
19-
}while(x!=-1);
22+
} while(x!=-1);
23+
24+
/*
25+
Creating a-array of Byte.
2026
21-
/* Creating a-array of Byte.
27+
byte b[]=new byte[fis.available()];
28+
*/
2229

23-
byte b[]=new byte[fis.available()]; *//*Checking how many bytes available in, input file.*//*
30+
/* Checking how many bytes available in, an input file. */
2431

32+
/*
2533
fis.read(b);
2634
//Byte array converting into a string.
2735
String str = new String(b);
2836
2937
//Read from file and print.
30-
System.out.println(str);*/
38+
System.out.println(str);
39+
*/
3140
}
3241
}
3342
}
43+
44+
/*
45+
1. FileInputStream:
46+
- Stream jo file se data read karne ke liye use hota hai.
47+
- Data binary form (bytes) me read hota hai.
48+
- Agar text file read karna ho → bytes ko char/string me convert karna padta hai.
49+
- Agar file exist nahi hai → `FileNotFoundException` throw hota hai.
50+
51+
2. try-with-resources:
52+
- Yaha `FileInputStream fis = new FileInputStream("path");`
53+
ko try block me declare kiya gaya hai.
54+
- try-with-resources ka benefit → `fis.close()` automatically call ho jaata hai.
55+
56+
3. Important Methods:
57+
- `fis.read()` → ek byte read karta hai, -1 return karta hai jab end of file (EOF) aata hai.
58+
- `fis.available()` → bataata hai kitne bytes abhi read ke liye bache hue hain.
59+
- `fis.read(byte[] b)` → ek baar me pura byte array fill kar deta hai.
60+
61+
4. is program me kya ho raha hai:
62+
- File open hui WithResources.txt ke liye.
63+
- `do-while` loop ke andar ek-ek byte read ho raha hai.
64+
- `(char)x` cast karke output me letter print kiya gaya hai.
65+
- Alternative approach bhi diya hai:
66+
- Simple `while((x=fis.read())!=-1)` loop.
67+
- Ya phir `fis.available()` se byte array banakar ek hi baar me pura content read karke `new String(b)` se
68+
print karna.
69+
70+
5. Use Cases:
71+
- Binary files (images, audio, executables) read karna.
72+
- Text files character by character read karna.
73+
- Agar character-stream handling (encoding safe) chahiye toh `FileReader` use karo.
74+
75+
76+
✔ FileInputStream = binary read stream.
77+
✔ read() → byte by byte read, -1 = EOF.
78+
✔ available() → kitne bytes pending hain.
79+
✔ try-with-resources = auto close, no need to call `fis.close()`.
80+
*/

0 commit comments

Comments
 (0)