Write to a File
To write in a File created, we use FileWriter class
together with the write() method to write some text into the file. Note
that once we are done with writing in the file, you should close it with the close()
method.
Example
package practices; import java.io.FileWriter; import java.io.IOException; public class WriteToFile { public static void main(String args[]){ try { FileWriter filewriter = new FileWriter("filename.txt"); filewriter.write("SparkDatabox is one of the best online training institute"); filewriter.close(); //must close after writing to file System.out.println("Successfully wrote to the file"); } catch (IOException e) { System.out.println("An erro occured"); e.printStackTrace(); } } }
Output
Successfully wrote to the file