Skip to content

Commit 61e20de

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

2 files changed

+8
-14
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Chill bro don't worry work your ass off
1+
Chill bro don't worry, work your ass off.

Section23JavaIOStreams/DemoCodes/src/Demo4.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
import java.io.IOException;
33

44
class Demo4 {
5-
public static void main(String[] args)
6-
{
7-
try
8-
{
9-
FileOutputStream fout = new FileOutputStream("C://Users//somes//Downloads//NoteApp//JavaEvolution-Learning-Growing-Mastering\\Section23JavaIOStreams\\DemoCodes\\TXT Files\\Demo4.txt");
5+
public static void main(String[] args) {
6+
try {
7+
FileOutputStream fout = new FileOutputStream("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/DemoCodes/TXT Files/Demo4.txt");
108

11-
String myText = "Chill bro don't worry work your ass off";
9+
String myText = "Chill bro don't worry, work your ass off.";
1210

1311
byte[] myData = myText.getBytes();
1412

@@ -20,16 +18,13 @@ public static void main(String[] args)
2018
//Writes 'A' into the file. ASCII Value.
2119
fout.close();
2220
}
23-
catch (IOException e)
24-
{
21+
catch (IOException e) {
2522
System.out.println(e);
2623
}
2724
}
2825
}
2926

3027
/*
31-
Quick Overview:
32-
3328
This program writes data into a file named myFile.txt using FileOutputStream.
3429
- It first prepares a string "Welcome to JAVA", converts it to bytes
3530
(though it doesn’t actually write it), and writes only a single byte 65 ('A' character) into the file.
@@ -47,8 +42,7 @@ public static void main(String[] args)
4742
- fout.write(65) writes the ASCII character 'A' into myFile.txt.
4843
- Yes, fout is a reference (not the actual object itself).
4944
50-
This how we use FileOutputstream class to write the data into file
45+
This how we use FileOutputstream class to write the data into file,
5146
by how you write data by streaming the data and how streaming a data by converting into bytes
52-
This is for write data into file using output stram
53-
47+
This is for write data into file using output stream.
5448
*/

0 commit comments

Comments
 (0)