Thursday, August 30, 2012

Java Program to Write Lines of Text to a File + Source Code


To learn the Basics of file manipulation in java ,the best way to start is by writing simple code to write some text to file.The following is a sample program that writes text to file using BufferedWriter Class

It uses the File Class to open the File to the BufferedWriter Class,which writes some sample text to the file.The main class used is the Writer Class intended for writing stream data to files.

Below is the complete source Code to implement a Simple File Writer in Java


 import java.io.*;  
 public class FileWriter {  
  public static void main(String[] args)throws IOException{  
  String towrite = "Hello World";  
  Writer data = null;  
  File file = new File("myfile.txtt");  
  data = new BufferedWriter(new FileWriter(file));  
  data.write(text);  
  data.close();  
  System.out.println("Text Written");   
  }  
 }  

2 comments:

Post a Comment

Subscribe

The Source Codes Published in this Blog can be used freely for Educational purposes but should not be reproduced on any other Blog or Website without the consent of the author.