Use Ramdonaccessfile to achieve the file append!

Source: Internet
Author: User
Access Ramdonaccessfile is a very useful class that is powerful and can take advantage of its


length () and Seek () methods to easily append files, believe me, the following example is


is easy to read, first write 10 lines, with length () read out in bytes (in byte),


is moved to the end of the file with Seek (), continues adding, and finally displays the record.





import java.io.*;


public class Iostreamdemo {


public static void Main (string[] args) {


try{


randomaccessfile Rf1 = new Randomaccessfile ("D:\\jeru.txt", "RW");


for (int i = 0; i < i + +) {


rf1.writebytes ("Xixi,this is line" +i+ "\ n");


      }


Rf1.close ();


  


int i = 0;


String record = new String ();


randomaccessfile Rf2 = new Randomaccessfile ("D:\\jeru.txt", "RW");


Rf2.seek (Rf2.length ());


rf2.writebytes ("Lala,append line" + "\ n");


Rf2.close ();


    


randomaccessfile Rf3 = new Randomaccessfile ("D:\\jeru.txt", "R");


while (record = Rf3.readline ())!= null) {


i + +;


System.out.println ("Value" +i+ ":" +record);


      }


Rf3.close ();


}catch (Exception e) {}


}


}





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.