Java development, occasionally encountered at the end of the file to append the contents of the file, there are actually many ways to achieve, a brief introduction of two:
One is implemented by the Randomaccessfile class, and the other by the FileWriter class.
The implementation method is very simple, there is nothing to say, look directly at the code:
1 Public Static voidMain (string[] args)throwsException {2File file=NewFile (filename);3Randomaccessfile raf=NewRandomaccessfile (file, "RW");4 //moves the write file pointer to the end of the file. 5 Raf.seek (Raf.length ());6Raf.writebytes ("First Test randomaccessfile append \ r \ n");7 raf.close ();8FileWriter writer =NewFileWriter (file,true);9Writer.write ("Test randomaccessfile append \ r \ n");Ten writer.close (); OneTA =NewRandomaccessfile (file, "RW"); A //moves the write file pointer to the end of the file. - Raf.seek (Raf.length ()); -Raf.writebytes ("Second Test randomaccessfile append \ r \ n"); the raf.close (); -}
Finally view the contents of the file:
It's so simple .....
Two ways to append content to the end of a Java file