Commit 484dd16
committed
refactor: add try-with-resources for safer file writing with FileOutputStream
WHAT:
- Implemented `FileExample2` demonstrating file writing using `FileOutputStream` inside a try-with-resources block.
- Wrote a string ("With Using Resources.") to a file after converting it into a byte array.
- Replaced manual `fos.close()` with automatic resource management.
- Included commented alternatives:
- Writing each byte individually in a loop.
- Writing partial content using `fos.write(byte[], offset, length)`.
WHY:
- Ensures automatic closing of the stream, reducing the risk of resource leaks.
- Simplifies error handling and makes the code cleaner.
- Encourages best practices in modern Java (Java 7+).
HOW:
1. Opened file using `try(FileOutputStream fos = new FileOutputStream(path)) { ... }`.
2. Converted string to bytes (`str.getBytes()`).
3. Wrote the byte array directly with `fos.write(b)`.
4. Stream automatically closed at the end of the try block.
BENEFITS:
- No need for explicit `close()` calls.
- Even if an exception occurs, the resource will be closed properly.
- Cleaner, more concise code compared to manual try-catch-finally.
REAL-WORLD USE CASES:
- Writing logs, reports, or configuration files.
- Safely handling file I/O in enterprise applications where resource leaks can be costly.
- Recommended for modern Java development wherever streams are used.
NOTES:
- Multiple resources (e.g., FileInputStream + BufferedInputStream) can be opened in the same try-with-resources block.
- For text-heavy operations, `FileWriter`/`BufferedWriter` may be preferable.
- For performance with large data, combine with `BufferedOutputStream`.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 6df6bce commit 484dd16
File tree
2 files changed
+48
-14
lines changed- Section23JavaIOStreams/src
- MyJAVA
2 files changed
+48
-14
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
| 4 | + | |
| 5 | + | |
9 | 6 | | |
10 | | - | |
11 | | - | |
| 7 | + | |
12 | 8 | | |
13 | 9 | | |
14 | 10 | | |
15 | 11 | | |
| 12 | + | |
16 | 13 | | |
17 | | - | |
| 14 | + | |
| 15 | + | |
18 | 16 | | |
19 | | - | |
20 | 17 | | |
21 | | - | |
22 | 18 | | |
23 | | - | |
24 | 19 | | |
| 20 | + | |
25 | 21 | | |
26 | 22 | | |
27 | 23 | | |
28 | 24 | | |
29 | 25 | | |
30 | 26 | | |
31 | 27 | | |
32 | | - | |
| 28 | + | |
| 29 | + | |
33 | 30 | | |
34 | | - | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
0 commit comments