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/>}