Skip to content

Commit 6df6bce

Browse files
committed
feat: implement basic file writing example using FileOutputStream
WHAT: - Added `FileExample` to demonstrate writing data into a file using `FileOutputStream`. - Writes a full string directly to a file using `fos.write(str.getBytes())`. - Included commented alternatives: - Writing bytes one-by-one in a loop. - Writing a substring portion using `fos.write(byte[], offset, length)`. WHY: - `FileOutputStream` is one of the core classes in Java I/O for writing raw bytes into files. - Understanding byte-level writing is important before working with higher-level abstractions like `BufferedWriter` or `PrintWriter`. HOW: 1. Opened a file at the given path (`Test.txt`) using `FileOutputStream`. 2. Converted a string into a byte array with `getBytes()`. 3. Wrote the byte array to the file in one go. 4. Closed the stream to release system resources. ERROR HANDLING: - Caught `FileNotFoundException` (e.g., if the path is invalid). - Caught general `IOException` for any unexpected I/O issues. REAL-WORLD USE CASES: - Writing logs or reports directly into a file. - Saving application data (text or binary) at runtime. - Building utilities that generate configuration or result files. - Basis for advanced file operations (serialization, buffering, streaming large datasets). NOTES: - Always close the stream (`fos.close()`) to avoid resource leaks. - Better approach: use try-with-resources (`try(FileOutputStream fos = ...) { ... }`) for auto-closing. - For character-specific writing, `FileWriter` or `BufferedWriter` is more convenient. - For frequent writes, consider `BufferedOutputStream` for performance improvements. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 76b0f29 commit 6df6bce

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

Section23JavaIOStreams/src/FileExample.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
public class FileExample {
55
public static void main(String[] args) {
66
try {
7-
FileOutputStream fos = new FileOutputStream("C://Users//somes//Downloads//JAVA SE//Section23JavaIOStreams//src//MyJAVA/Test.txt");
8-
String str = "Learn JAVA Programming";
7+
FileOutputStream fos = new FileOutputStream("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/src/MyJAVA/Test.txt");
8+
String str = "Learn JAVA Programming From Abdul Bari Sir and Engineering Digest YT";
99

1010
/*
1111
other methods try in other-program.
@@ -25,4 +25,35 @@ public static void main(String[] args) {
2525
System.out.println(r);
2626
}
2727
}
28-
}
28+
}
29+
30+
/*
31+
1. FileOutputStream:
32+
- Stream jo file ke andar data likhne (write) ke liye use hota hai.
33+
- Data binary format me write hota hai (characters ko bhi byte array me convert karke likhta hai).
34+
- Agar file exist nahi karti → nayi file create ho jaati hai.
35+
- Agar file already exist karti hai → purana content overwrite ho jaata hai.
36+
37+
2. Important Methods:
38+
- fos.write(int b) → ek single byte likhta hai.
39+
- fos.write(byte[] b) → poora byte array likhta hai.
40+
- fos.write(byte[] b, int off, int len) → array ke ek portion ko write karta hai.
41+
- fos.close() → stream band karna zaroori hai taaki resources release ho jayein.
42+
43+
3. is program me:
44+
- `FileOutputStream fos = new FileOutputStream("path.../Test.txt");`
45+
→ File open/create kiya writing ke liye.
46+
- `str.getBytes()` → String ko byte array me convert kiya.
47+
- `fos.write(str.getBytes());` → pura string file me write kiya.
48+
- `fos.close();` → file properly close ki gayi.
49+
50+
4. Use Cases:
51+
- Text ya binary file banani ho.
52+
- Logs, reports ya serialized data likhne ke liye.
53+
- Agar character-based writing chahiye toh `FileWriter` use karna better.
54+
55+
56+
✔ FileOutputStream = binary write stream.
57+
✔ getBytes() → String → byte array.
58+
✔ Overwrites file by default (append karna ho toh `new FileOutputStream(path, true)`).
59+
*/
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Learn JAVA Programming
1+
Learn JAVA Programming From Abdul Bari Sir and Engineering Digest YT

0 commit comments

Comments
 (0)