ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

How to write to a file or a plain text document?

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");
  16.  
  17. }
  18. }
  19.  
  20.  

OUTPUT:

Write Operation successful

——————————————————-
In the above piece of code we have used BufferWriter class and FileWriter class for writing into a plain text document.
With significant increase of software product in the market, requirement of client vary from one software to another. You just need to know the basic which is mention above. Rest you can manipulate and modify the code based on your requirement.

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!