Saturday, June 9, 2007

Writing a Content to a file in Java


import java.io.*;

public class FileWriterDemo
{
public static void main(String args[]) throws FileNotFoundException,IOException
{
File file=new File("Demo.txt"); // or u can use file name with path eg. D:\\Demo\\Demo.txt
if(!file.exists())
file.createNewFile();

BufferedWriter out = new BufferedWriter(new FileWriter(file,true));// true flag tells us to append to the exiting file.
out.write("Hello World.");
out.newLine();
out.close();
}

}
Save above contents in a file called FileWriterDemo.java

No comments:

Post a Comment