Skip to content

Commit 7c49837

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

2 files changed

+44
-15
lines changed
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
This Java program reads data from a file named myFile.txt,
2-
character by character, and prints each character on a new line.
1+
This Java program reads data from a file named myFile.txt, character by character, and prints each character on a new line.
32

4-
It uses FileInputStream and handles I/O exceptions if the file doesn't exist or can't be read.
3+
It uses FileInputStream and handles I/O exceptions if the file doesn't exist or can't be read.
Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,54 @@
1-
//Now Data Read in File
21
import java.io.IOException;
32
import java.io.FileOutputStream;
43
import java.io.FileInputStream;
54

6-
class Demo2
7-
{
8-
public static void main(String[] args)
9-
{
10-
try
11-
{
5+
class Demo2 {
6+
public static void main(String[] args) {
7+
try {
128
int data;
13-
FileInputStream fin = new FileInputStream("C://Users//somes//Downloads//NoteApp//JavaEvolution-Learning-Growing-Mastering//Section23JavaIOStreams//DemoCodes\\TXT Files\\Demo2.txt");
14-
while( (data = fin.read()) != -1)
15-
{
9+
FileInputStream fin = new FileInputStream("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/DemoCodes/TXT Files/Demo2.txt");
10+
while ((data = fin.read()) != -1) {
1611
System.out.println( (char) data);
1712
}
1813
}
19-
catch (IOException e)
20-
{
14+
catch (IOException e) {
2115
System.out.println(e);
2216
}
2317
}
2418
}
19+
20+
/*
21+
1. FileInputStream:
22+
- Ye ek byte-oriented stream hai jo file ka content byte by byte read karta hai.
23+
- Text files, images, audio, binary files sab ke liye use ho sakta hai.
24+
25+
2. Code Explanation:
26+
- `FileInputStream fin = new FileInputStream("path");`
27+
→ Ye file ko open karta hai read karne ke liye.
28+
- `fin.read()` ek byte return karta hai (0–255 int).
29+
- Jab end of file (EOF) aata hai, to -1 return hota hai.
30+
31+
3. Loop:
32+
- `while ((data = fin.read()) != -1)`
33+
- Har iteration me ek byte read hota hai aur `(char)data` se usko character me convert karke print karte ho.
34+
- Yahan `System.out.println((char)data);` likha hai → har ek character **nayi line me** print hoga.
35+
Agar same line me chahiye to `System.out.print((char)data);` use karo.
36+
37+
4. Exception Handling:
38+
- Agar file nahi mili ya read karte waqt error aaya, to `IOException` catch block me handle hota hai.
39+
- `System.out.println(e);` error message console par dikhata hai.
40+
41+
5. Output:
42+
- File ke andar jitna content hai wo line by line (char by char) console me print hoga.
43+
- Example: File me `"Hello"` likha hai → Output:
44+
H
45+
e
46+
l
47+
l
48+
o
49+
50+
- `fin.close()` call karna zaroori hai after reading, warna file handle release nahi hoga (resource leak).
51+
- Tumhare code me `fin.close()` missing hai. Better hai try-with-resources use karna (Java 7+).
52+
- Agar tumhe performance chahiye, to `BufferedInputStream` ya `BufferedReader` prefer karo
53+
(kyunki wo ek-ek byte ke bajay buffer me read karte hain).
54+
*/

0 commit comments

Comments
 (0)