How does Java read the last record of a file and append the record at the end of the file?

Source: Internet
Author: User

1. Read the last row of the file. Of course, this method is faster than the following one.

Randomaccessfile Rf = NULL; <br/> system. out. println (new date (). gettime (); <br/> try {<br/> Rf = new randomaccessfile ("D: // you. log "," R "); <br/> long Len = RF. length (); <br/> long start = RF. getfilepointer (); <br/> long nextend = start + len-1; <br/> string line = ""; <br/> RF. seek (nextend); <br/> int c =-1; <br/> int T = 0; <br/> while (nextend> start) {<br/> C = RF. read (); <br/> If (C = '/N' | C =' /R') {<br/> line = RF. Readline (); <br/> T ++; <br/> If (T> = 1 & line! = NULL) {<br/> system. out. println (line); <br/> return; <br/>}< br/> nextend --; <br/> RF. seek (nextend); <br/> If (nextend = 0) {// when the file pointer is redirected to the beginning of the file, the first line is output. <br/> system. out. println (RF. readline (); <br/>}< br/>} catch (filenotfoundexception e) {<br/> E. printstacktrace (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}finally {<br/> system. out. println (new date (). ge TTime (); <br/> try {<br/> If (rf! = NULL) <br/> RF. close (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/>}

2. Read the last record of the file

String string2 = NULL; <br/> try {<br/> filereader Fr = new filereader ("D: // you. log "); <br/> bufferedreader BR = new bufferedreader (FR); <br/> string STR = NULL; <br/> while (STR = BR. readline ())! = NULL) {<br/> string2 = STR; <br/>}< br/> Br. close (); <br/> Fr. close (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> system. out. println (string2 );

3. append records at the end of the file

Try {<br/> // open a file writer. The second parameter true in the constructor indicates that the file is written as an append. <br/> filewriter writer = new filewriter ("D: // you. log ", true); <br/> writer. write ("write data in the last row! "+"; "+"/R/N "); <br/> writer. close (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}

4. append records at the end of the file

Try {<br/> randomaccessfile Rf = new randomaccessfile ("D: // you. log "," RW "); <br/> Long Count = RF. length (); <br/> system. out. println ("write data in the last row! "); <Br/> RF. seek (count); </P> <p> // to wrap a line, use the/R/n sequence. <br/> RF. writebytes ("ABC/R/N"); // It is not garbled when writing characters. If you need to write Chinese characters, use the method of the following line </P> <p> string strfile = new string ("write data to gfgfgf in the last line! ". Getbytes (), "ISO_8859-1"); <br/> RF. writebytes (strfile + "/R/N"); <br/> RF. close (); </P> <p >}catch (exception e) {<br/> system. err. println ("read/write error"); <br/>}

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.