|
| 1 | +import java.io.FileOutputStream; |
| 2 | +import java.io.IOException; |
| 3 | + |
| 4 | +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"); |
| 10 | + |
| 11 | + String myText = "Chill bro don't worry work your ass off"; |
| 12 | + |
| 13 | + byte[] myData = myText.getBytes(); |
| 14 | + |
| 15 | + System.out.println("Data Written"); |
| 16 | + |
| 17 | + fout.write(myData); |
| 18 | + |
| 19 | + //fout.write(65); //Override the ASCII Value |
| 20 | + //Writes 'A' into the file. ASCII Value. |
| 21 | + fout.close(); |
| 22 | + } |
| 23 | + catch (IOException e) |
| 24 | + { |
| 25 | + System.out.println(e); |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/* |
| 31 | +Quick Overview: |
| 32 | +
|
| 33 | +This program writes data into a file named myFile.txt using FileOutputStream. |
| 34 | +- It first prepares a string "Welcome to JAVA", converts it to bytes |
| 35 | +(though it doesn’t actually write it), and writes only a single byte 65 ('A' character) into the file. |
| 36 | +- It uses streaming (byte-by-byte) to write data. |
| 37 | +
|
| 38 | +- fout is a reference to the FileOutputStream object. |
| 39 | +
|
| 40 | +FileOutputStream → The class (type). |
| 41 | +new FileOutputStream(...) → Creates a new object in memory. |
| 42 | +So fout is not the object itself—it's like a remote control or pointer that lets you |
| 43 | +interact with the FileOutputStream object created by new. |
| 44 | +
|
| 45 | +Extra Quick Notes: |
| 46 | +- getBytes() converts the string into a byte array (but here it's not actually used to write). |
| 47 | +- fout.write(65) writes the ASCII character 'A' into myFile.txt. |
| 48 | +- Yes, fout is a reference (not the actual object itself). |
| 49 | +
|
| 50 | +This how we use FileOutputstream class to write the data into file |
| 51 | +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 | +
|
| 54 | +*/ |
0 commit comments