Java Append write TXT file

Source: Internet
Author: User

Tag: write string except defragment long ring write ATI OID

Collated the information on the Internet, data appended to the TXT file three ways, see the following code:

Method One:

1  Public voidmethod1 () {2FileWriter FW =NULL;3 Try {4 //Append Content If the file exists, or create file if the file does not exist5File f=NewFile ("E:\\dd.txt");6FW =NewFileWriter (F,true);7}Catch(IOException e) {8 e.printstacktrace ();9 }TenPrintWriter PW =NewPrintWriter (FW); OnePw.println ("Append content"); A Pw.flush (); - Try { - Fw.flush (); the pw.close (); - fw.close (); -}Catch(IOException e) { - e.printstacktrace (); + } -}

Method Two:

1  Public Static voidmethod2 (string file, String conent) {2BufferedWriter out =NULL;3 Try {4out =NewBufferedWriter (NewOutputStreamWriter (5 NewFileOutputStream (file,true)));6Out.write (conent+ "\ r \ n");7}Catch(Exception e) {8 e.printstacktrace ();9}finally {Ten Try { One out.close (); A}Catch(IOException e) { - e.printstacktrace (); - } the } -}

Method Three:

1  Public Static voidmethod3 (String fileName, string content) {2 Try {3 //open a random Access file stream, read and write4Randomaccessfile Randomfile =NewRandomaccessfile (FileName, "RW");5 //file length, number of bytes6 LongFilelength =randomfile.length ();7 //moves the write file pointer to the end of the file. 8 Randomfile.seek (filelength);9Randomfile.writebytes (content+ "\ r \ n");Ten randomfile.close (); One}Catch(IOException e) { A e.printstacktrace (); - } - } the}

Java Append write TXT file

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.