Commit 61e20de
committed
feat: implement file writing with FileOutputStream (Demo4)
WHAT:
- Added a program that demonstrates writing data into a file using
`FileOutputStream`.
- Writes both a full string (converted to bytes) and optionally a single
character via ASCII value.
HOW IT WORKS:
1. Create a `FileOutputStream` pointing to Demo4.txt.
2. Define a string: "Chill bro don't worry, work your ass off."
3. Convert the string into a byte array using `getBytes()`.
4. Write the byte array to the file via `fout.write(myData)`.
5. Optionally, `fout.write(65)` could be used to write the ASCII value
'A'.
6. Close the stream to release resources.
KEY POINTS:
- `FileOutputStream` is a byte stream → writes raw bytes into a file.
- `getBytes()` converts a string into its byte representation.
- Closing the stream (`fout.close()`) is mandatory to avoid resource
leaks and ensure data flush.
- The reference `fout` is just a handle to the actual
`FileOutputStream` object created via `new`.
REAL-WORLD USE CASES:
- Writing logs or plain text to files.
- Saving user input or configurations.
- Exporting data in binary format (images, PDFs, etc.).
- Forming the base for higher-level abstractions like `FileWriter` or
`BufferedWriter`.
NOTES:
- By default, `FileOutputStream` overwrites file content.
- Use the constructor with `true` as the second argument
(`new
FileOutputStream(path, true)`) for append mode.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 7a38286 commit 61e20de
File tree
2 files changed
+8
-14
lines changed- Section23JavaIOStreams/DemoCodes
- TXT Files
- src
2 files changed
+8
-14
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
10 | 8 | | |
11 | | - | |
| 9 | + | |
12 | 10 | | |
13 | 11 | | |
14 | 12 | | |
| |||
20 | 18 | | |
21 | 19 | | |
22 | 20 | | |
23 | | - | |
24 | | - | |
| 21 | + | |
25 | 22 | | |
26 | 23 | | |
27 | 24 | | |
28 | 25 | | |
29 | 26 | | |
30 | 27 | | |
31 | | - | |
32 | | - | |
33 | 28 | | |
34 | 29 | | |
35 | 30 | | |
| |||
47 | 42 | | |
48 | 43 | | |
49 | 44 | | |
50 | | - | |
| 45 | + | |
51 | 46 | | |
52 | | - | |
53 | | - | |
| 47 | + | |
54 | 48 | | |
0 commit comments