Package com. Practice_filewriter;import Java.io.filewriter;import java.io.IOException; Public classJustpractice { Public Static voidMain (string[] args) throws IOException {//The first step is to create an output stream object that can write data to the file, which is the path to write to the file;FileWriter FW =NewFileWriter ("D:\\fw.txt"); //In the second step, the write method of the FW is called, and the data is written to the file;Fw.write (" haha haha haha");//at this point the data is actually written to the temporary file buffer, and did not write to the file you specified,//The third step is to call the flush () method of the FileWriter object to write the data to the specified fileFw.flush (); //Finally, call the Close method to close the output stream, which automatically calls the previous Flush method and writes the buffer data to the destination file .Fw.close (); }}
The write () method of the class reader in the Java.io package, all classes that have the suffix reader inherit these methods:
Javaio writing data to a file