Several ways to write files in Java

Source: Internet
Author: User

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)

 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); BufferedWriter Bufferwritter=NewBufferedWriter (Filewritter);             Bufferwritter.write (data);         Bufferwritter.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.

 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.

 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 write files in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.