Skip to content

Commit 7ab1469

Browse files
committed
feat: write string data to file using FileOutputStream (Demo5)
WHAT: - Implemented a program that demonstrates how to write an entire string into a file using `FileOutputStream`. - File used: Demo5.txt HOW: 1. Created a `FileOutputStream` pointing to Demo5.txt. 2. Defined a string: "converting string into byte array". 3. Converted the string into a byte array via `getBytes()`. 4. Wrote the byte array to the file using `fout.write(b)`. 5. Closed the stream to free system resources. 6. Printed "success" on successful write. KEY POINTS: - `FileOutputStream` works with bytes, so strings must be converted into byte arrays (`String.getBytes()`). - Closing the stream is essential to avoid resource leaks and flush data to disk. - By default, opening a file with `FileOutputStream` overwrites existing content. REAL-WORLD USE CASES: - Persisting string-based data (logs, user input, configuration). - Writing data from memory into a plain text file. - Base building block for advanced file writing (e.g., buffered streams, file writers). - Converting any string to bytes and saving in binary files (beyond just text). NOTES: - To append instead of overwrite, use: `new FileOutputStream(path, true)`. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 61e20de commit 7ab1469

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Living the Dreams
1+
converting string into byte array
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
import java.io.FileOutputStream;
2-
public class Demo5
3-
{
4-
public static void main(String args[])
5-
{
6-
try
7-
{
8-
FileOutputStream fout=new FileOutputStream("C:\\Users\\somes\\Downloads\\NoteApp\\JavaEvolution-Learning-Growing-Mastering\\Section23JavaIOStreams\\DemoCodes\\TXT Files\\Demo5.txt");
9-
String s="Living the Dreams";
10-
byte b[]=s.getBytes(); //converting string into byte array
2+
public class Demo5 {
3+
public static void main(String args[]) {
4+
try {
5+
FileOutputStream fout=new FileOutputStream("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/DemoCodes/TXT Files/Demo5.txt");
6+
7+
String s="converting string into byte array";
8+
9+
byte b[]=s.getBytes();
10+
1111
fout.write(b);
1212
fout.close();
13+
1314
System.out.println("success");
1415
}
15-
catch(Exception e)
16-
{
16+
catch(Exception e) {
1717
System.out.println(e);
1818
}
1919
}
2020
}
21+
2122
/*
2223
getBytes --> Encodes this String into a sequence of bytes using the default charset, storing the result into a new byte array.
2324
2425
This program writes the full string "Welcome to JAVA" into a file named myFile.txt using FileOutputStream.
2526
The string is converted into a byte array using getBytes().
26-
The entire byte array is written to the file.
27-
Displays "success" when done.
27+
The entire byte array is written to the file. Displays "success" when done.
2828
*/

0 commit comments

Comments
 (0)