Java three methods append content to end of file

Source: Internet
Author: User
Package org.outman.dms.server;   
    Import Java.io.BufferedWriter;   
    Import Java.io.FileOutputStream;   
    Import Java.io.FileWriter;   
    Import java.io.IOException;   
    Import Java.io.OutputStreamWriter;   
      
    Import Java.io.RandomAccessFile; /** * Description: Append content to end of file * @author Administrator */public class Writestreamappend {/  
        * * Append files: Using FileOutputStream, when constructing FileOutputStream, set the second argument to True * * @param fileName * @param content */public static void Method1 (string file, String conent) {Bufferedwr   
           ITER out = null; try {out = new BufferedWriter (New OutputStreamWriter FileOutputStream (fil   
              E, true));   
           Out.write (conent);   
           catch (Exception e) {e.printstacktrace (); Finally {try {out.close ();  
               catch (IOException e) {e.printstacktrace ();  
        }}/** * Append files: Using filewriter * * @param fileName   
           * @param content */public static void Method2 (string fileName, string content) { try {//Open a write file, the second argument in the constructor is true to write the file in append form FileWriter writer = new FileWriter (Filenam   
               E, true);   
               Writer.write (content);   
           Writer.close ();   
           catch (IOException e) {e.printstacktrace ();             }/** * Append files: Using randomaccessfile * * @param fileName * FileName * @param content * Append/public static void Method3 (S)   
           Tring FileName, String content) {try {//Open a random Access file stream, read-write    Randomaccessfile randomfile = new Randomaccessfile (fileName, "RW");   
               File length, byte number long filelength = Randomfile.length ();   
               Moves the write file pointer to the end of the file.  
              Randomfile.seek (filelength); 
               String S2=new string (content.getbytes ("GBK"), "iso8859-1");
               Randomfile.writebytes (S2);   
           Randomfile.close ();   
           catch (IOException e) {e.printstacktrace ();   
           } public static void Main (string[] args) {System.out.println ("start");   
           Method1 ("C:/work/test.txt", "Append to end of file 1"); 
           METHOD2 ("C:/work/test.txt", "Append to end of File 2");   
  
          Method3 ("C:/work/test.txt", "Append to end of File 3");   
       System.out.println ("End");   }   
     
   }

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.