hi,
i have a project in which there is my classes, i want to create a class which will store the data in to a file. data are manipulation in different class.
please tell me how to create that class which store the data.
Printable View
hi,
i have a project in which there is my classes, i want to create a class which will store the data in to a file. data are manipulation in different class.
please tell me how to create that class which store the data.
You use the FileWriter and BufferedWriter class:
Class FileWriter
The FileWriter is a class used for writing character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. To specify these values yourself, construct an OutputStreamWriter on a FileOutputStream.
BufferedWriter
The BufferWriter 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.
Example
Code:import java.io.*;
class FileWrite
{
public static void main(String args[])
{
FileWriter fstream = new FileWriter("name.txt"); //create a file called name.txt
BufferedWriter out = new BufferedWriter(fstream); // create a writing stream
out.write("Hello Java"); //write whatever you want
out.close(); // stops writing
}
}
--------------------Configuration: <Default>--------------------
C:\FileWrite.java:7: unreported exception java.io.IOException; must be caught or declared to be thrown
FileWriter fstream = new FileWriter("name.txt"); //create a file called name.txt
^
C:\FileWrite.java:9: unreported exception java.io.IOException; must be caught or declared to be thrown
out.write("Hello Java"); //write whatever you want
^
C:\FileWrite.java:10: unreported exception java.io.IOException; must be caught or declared to be thrown
out.close(); // stops writing
^
3 errors
Process completed.
hehe i've just got this when I copy paste your code
ohh I forgot to right the throws IOException beside public static void main (String []args) __(>throws IOException<)__