How to Write to a File or a Plain Text Document


This tutorial is all about How to Write to a File or a Plain Text Document.

There are multiple ways to write data into text file or plain text file. We will learn the most standard way of writing into a text file.

Let us take an example:

  1. public class Manager{
  2. public static void main(String[] args) throws IOException{
  3. //Whenever we are dealing with any sort of input output operation
  4. //it is desirable to throws IOException as this is checked exception
  5. File f=new FIle("demo.txt"); //File Object
  6. BufferWriter bw=new BufferWrite(out); //BufferWriter Object
  7. bw.write("abc");
  8. bw.write("xyz");
  9. bw.newLine(); // to change the line in the text document newLine method is used
  10. bw.write("123");
  11. bw.write("678");
  12. bw.flush(); //this will save the text document
  13. bw.close();
  14. out.close(); //close method with this twoi object will avoid any kind of memory leaks.
  15. System.out.println ("Write Operation successful");
  17. }
  18. }


Write Operation successful



Readers might real also:






Facebook Comments


Please enter your comment!
Please enter your name here