Java Coder
How to Write to file in Java?
1 Answer(s)      2 years and a month ago
Posted in : Java Beginners

How to Write to file in Java Program?

Advertisement
View Answers

March 15, 2013 at 4:56 PM


Hi,

For writing a file in Java, we need some classes from Java.IO package. Mostly we define two types of classes in java file i.e. FileWriter and BufferedWriter, where as the FileWrite class is used for defining streams of characters and the BufferedWriter is used for store the characters in a buffer section so that we to write into a character-output stream.

You can Visit this link: http://www.roseindia.net/java/examples/io/writeToFile.shtml

or

Find the Examples of How to write to File in Java Program:

WriteToFileExample.java

import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
class WriteToFileExample 
{
public static void main(String args[])
{
BufferedWriter br= null;
try
{
File file = new File("fileWriter.txt");
FileWriter fw = new FileWriter(file);
/* You can also give the path as C:\\Desktop\\fileWriter.txt */
BufferedWriter br = new BufferedWriter(fw);
br.write("This example demonstrates you how to write in a file.");
br.close();
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
}
}