Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners How to Write to file in Java?
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Java Coder
How to Write to file in Java?
1 Answer(s)      a year and 9 months 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());
}
}
}


Related Tutorials/Questions & Answers:
Advertisements
 

 

 

DMCA.com