Transferred from: http://www.jb51.net/article/47062.htm
One, Filewritter write file
Filewritter, character Stream writes characters to a file. By default, it replaces all existing content with the new content, however, when you specify a true (Boolean) value as the second parameter of the Filewritter constructor, it preserves the existing content and appends the new content to the end of the file.
1. Replace all existing content with new content.
New FileWriter (file);
2. Keep the existing content and append the new content at the end of the file.
New FileWriter (file,true);
Append file Example
A text file that is named "Javaio-appendfile.txt" and contains the following content.
ABC Hello append new content FileWriter (file,true)
The code is as follows:
PackageCom.yiibai.file;ImportJava.io.File;ImportJava.io.FileWriter;ImportJava.io.BufferedWriter;Importjava.io.IOException; Public classAppendtofileexample { Public Static voidMain (string[] args) {Try{String Data= "This content would append to the end of the file"; File File=NewFile ("Javaio-appendfile.txt"); //if file doesnt exists, then create it if(!file.exists ()) {File.createnewfile (); } //true = Append fileFileWriter Filewritter =NewFileWriter (File.getname (),true);filewritter.write (data); Filewritter.close (); System.out.println ("Done"); }Catch(IOException e) {e.printstacktrace (); } }}
Results
The text file "Javaio-appendfile.txt" content is now updated as follows:
ABC Hello This content would append to the end of the file
Two, bufferedwriter write file
The buffered character (BufferedWriter) is a character stream class that handles the characters data. Unlike byte streams (which convert data into bytes), you can write strings, arrays, or character data to a file directly.
Copy CodeThe code is as follows:
PackageCom.yiibai.iofile;ImportJava.io.BufferedWriter;ImportJava.io.File;ImportJava.io.FileWriter;Importjava.io.IOException; Public classWritetofileexample { Public Static voidMain (string[] args) {Try{String content= "The content to write into file"; File File=NewFile ("/users/mkyong/filename.txt"); //if file doesnt exists, then create it if(!file.exists ()) {File.createnewfile (); } FileWriter FW=NewFileWriter (File.getabsolutefile ()); BufferedWriter BW=NewBufferedWriter (FW); Bw.write (content); Bw.close (); System.out.println ("Done"); } Catch(IOException e) {e.printstacktrace (); } }}
Three, FileOutputStream write file
The file output stream is a byte stream class used to process raw binary data. In order to write data to a file, the data must be converted to bytes and saved to a file. Please see the complete example below.
Copy CodeThe code is as follows:
PackageCom.yiibai.io;ImportJava.io.File;ImportJava.io.FileOutputStream;Importjava.io.IOException; Public classWritefileexample { Public Static voidMain (string[] args) {FileOutputStream fop=NULL; File file; String content= "This is the text content"; Try{file=NewFile ("C:/newfile.txt"); FOP=Newfileoutputstream (file); //if file doesnt exists, then create it if(!file.exists ()) {File.createnewfile (); } //get the content in bytes byte[] Contentinbytes =content.getbytes (); Fop.write (contentinbytes); Fop.flush (); Fop.close (); System.out.println ("Done"); } Catch(IOException e) {e.printstacktrace (); } finally { Try { if(FOP! =NULL) {fop.close (); } } Catch(IOException e) {e.printstacktrace (); } } }}
Updated JDK7 For example, use the new "Try resource off" method to easily work with files.
PackageCom.yiibai.io;ImportJava.io.File;ImportJava.io.FileOutputStream;Importjava.io.IOException; Public classWritefileexample { Public Static voidMain (string[] args) {File file=NewFile ("C:/newfile.txt"); String content= "This is the text content"; Try(FileOutputStream FOP =Newfileoutputstream (file)) { //if file doesn ' t exists, then create it if(!file.exists ()) {File.createnewfile (); } //get the content in bytes byte[] Contentinbytes =content.getbytes (); Fop.write (contentinbytes); Fop.flush (); Fop.close (); System.out.println ("Done"); } Catch(IOException e) {e.printstacktrace (); } }}
Several ways to share Java writing files