Skip to content

Commit 2758311

Browse files
committed
feat: add FileInputStream demo to read file byte by byte
WHAT: - Implemented a simple file I/O example using `FileInputStream`. - Opened `Demo1.txt` and read data one byte at a time. - Printed each byte as a character to the console. - Ensured proper resource cleanup by explicitly calling `fin.close()`. - Wrapped in try-catch to handle file not found and I/O errors gracefully. KEY LEARNINGS: 1. FileInputStream - A byte stream for reading raw bytes from a file. - Works for both text and binary data (images, audio, video). - `fin.read()` → returns an int [0–255] or -1 (EOF). 2. Conversion and Loop - `(char)i` converts numeric byte value to character for printing. - While loop continues until EOF is reached (-1). 3. Exception Handling - Prevents crashes if file path is wrong or I/O issues occur. - Closing stream (`fin.close()`) is essential to free system resources. 4. Best Practices - Prefer `try-with-resources` (Java 7+) → auto-closes file stream. - Use `FileReader`/`BufferedReader` for text files (encoding-aware). - Use `FileInputStream` for binary files (e.g., PDFs, images). REAL-LIFE APPLICATIONS: - Reading configuration or log files. - Streaming binary data (images, audio, video). - Implementing custom parsers for protocols or file formats. - Low-level utilities (e.g., checksum calculators, file encryption). Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 762312f commit 2758311

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
//This Java program reads a text file character by character using FileInputStream and prints its content to the console.
1+
This Java program reads a text file character by character using FileInputStream and prints its content to the console.
Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,54 @@
11
import java.io.FileInputStream;
2-
public class Demo1
3-
{
2+
public class Demo1 {
43
public static void main(String args[]){
5-
try
6-
{
7-
FileInputStream fin=new FileInputStream("C://Users//somes//Downloads//NoteApp//JavaEvolution-Learning-Growing-Mastering\\Section23JavaIOStreams\\DemoCodes\\TXT Files\\Demo1.txt");
8-
int i=0;
4+
try {
5+
FileInputStream fin = new FileInputStream
6+
("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/DemoCodes/TXT Files/Demo1.txt");
97

10-
while((i=fin.read())!=-1)
11-
{
8+
int i=0;
9+
while((i=fin.read())!=-1) {
1210
System.out.print((char)i);
1311
}
14-
15-
/*
16-
Reads the file one byte at a time.
17-
fin.read() returns an int representing the byte value. It returns -1 when the end of the file (EOF) is reached.
18-
(char)i converts the byte into a character, and it prints it to the console.
19-
So, this part prints the entire content of the file.
20-
*/
2112
fin.close();
2213
}
23-
catch(Exception e)
24-
{
14+
catch(Exception e) {
2515
System.out.println(e);
2616
}
2717
}
2818
}
19+
20+
/*
21+
Reads the file one byte at a time.
22+
fin.read() returns an int representing the byte value. It returns -1 when the end of the file (EOF) is reached.
23+
(char)i converts the byte into a character, and it prints it to the console.
24+
So, this part prints the entire content of the file.
25+
26+
1. FileInputStream:
27+
- Ye ek byte stream hai jo file ke andar ka data byte by byte read karta hai.
28+
- Mostly text files, image files, audio/video streams ke liye use hota hai.
29+
30+
2. Code Explanation:
31+
- `FileInputStream fin = new FileInputStream("path");`
32+
→ Ye file ko open karta hai given path par.
33+
- `fin.read()` ek-ek byte return karta hai (0-255 integer value).
34+
- Jab end of file (EOF) aata hai, to -1 return hota hai.
35+
- `(char)i` cast karke hum us integer ko character me convert karke print karte hain.
36+
- `fin.close()` resource (file handle) free kar deta hai.
37+
38+
3. Loop Working:
39+
- while loop tab tak chalega jab tak file khatam nahi hoti.
40+
- Har byte → char me cast → console par print.
41+
42+
4. Exception Handling:
43+
- Agar file nahi mili (FileNotFoundException) ya koi I/O error aaya
44+
→ catch block run hoga aur error print karega.
45+
46+
5. Output:
47+
- File me jo bhi likha hai, wahi console par as-it-is print ho jaayega.
48+
49+
- Always close the stream (`fin.close()`) warna resource leak ho sakta hai.
50+
- Better approach: try-with-resources (Java 7+), jisme close automatic ho jaata hai.
51+
- Text files ke liye `FileReader` / `BufferedReader` zyada convenient hote hain
52+
(kyunki wo character streams hai, encoding friendly).
53+
- FileInputStream mainly tab use hota hai jab tumhe binary data (images, audio, pdf) read karna ho.
54+
*/

0 commit comments

Comments
 (0)