Top News

Java File - 02

 Java File  - 02

Today we have to lean about how t o write the data into the file. Previous post we have leaned how do we create file via Java Language. I will share the Link below this post. if you ae not reading that post please go and read first of all.




So now we will do some Insertion work into that file.

// importing the FileWriter class
import java.io.FileWriter;
public class file {
    public static void main(String args[]) {

        String data = "This is the data in the output file";
        try {
          // Creates a Writer using FileWriter
          FileWriter output = new FileWriter("csetechtalkswithshaa.txt");
   
          // Writes string to the file
          output.write(data);
          System.out.println("Data is written to the file.");
   
          // Closes the writer
          output.close();
        }
        catch (Exception e) {
          e.getStackTrace();
        }
     }  
}


import java.io.FileWriter;
this is the line support to create writer to add data.

also we have create data variable with Sting data types. In that data has value of
"This is the data in the output file".




then we ae creating out variable to store the file.
File name is csetechtalkswithshaa.txt



initially we don't have any data inside the file.

once write the data then message will
output like "Data is Witten to the file ".

finally we have to close the output.

 appreciate you taking the time to read this post. Don't forget to check back for more interesting content in the near future. See you next time!

Post a Comment

Previous Post Next Post