For writing data to a file, the class FileWriter and BufferedWriter are used.
FileWriter is a subclass of OutputStreamWriter class that is used to write text (as opposed to binary data) to a file. You create a FileWriter by specifying the file to be written to, or optionally, when the data should be appended to the end of an existing file instead of overwriting that file. The FileWriter class creates an internal FileOutputStream to write bytes to the specified file
The BufferedWriter class is used to write text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters,
arrays and strings.
The constructor of the FileWriter class takes the file name which has to be buffered by the BufferedWriter stream. The write( ) method of BufferedWriter class is used to create the file into specified directory.
Following code write data into new file:
Following code creates the object of FileWriter and BufferedWriter:
|FileWriter fstream = new FileWriter(file_name);
BufferedWriter out = new BufferedWriter(fstream);
Lets see an another example that writes the text input by the user using the FileWriter and the BufferedWriter class.
Output of the Program
Please enter the file name to create : nishu.txt
this is my name
File created successfully.
We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.
Posted on: April 17, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles