How to Write to a File or a Plain Text Document

0
130

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:

[java]

public class Manager{
public static void main(String[] args) throws IOException{
//Whenever we are dealing with any sort of input output operation
//it is desirable to throws IOException as this is checked exception
File f=new FIle(“demo.txt”); //File Object
FileWriter out=new FileWriter(f);
BufferWriter bw=new BufferWrite(out); //BufferWriter Object
bw.write(“abc”);
bw.write(“xyz”);
bw.newLine(); // to change the line in the text document newLine method is used
bw.write(“123”);
bw.write(“678”);
bw.flush(); //this will save the text document
bw.close();
out.close(); //close method with this twoi object will avoid any kind of memory leaks.
System.out.println (“Write Operation successful”);

}
}

 

[/java] OUTPUT:

Write Operation successful


 

 

Readers might real also:

 

 

 

 

 

Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.