ImportJava.io.BufferedWriter; ImportJava.io.FileOutputStream; ImportJava.io.FileWriter; Importjava.io.IOException; ImportJava.io.OutputStreamWriter; ImportJava.io.RandomAccessFile; Public classWritestreamappend { Public Static voidmethod1 (string file, String conent) {BufferedWriter out=NULL; Try{ out=NewBufferedWriter (NewOutputStreamWriter (NewFileOutputStream (file,true))); Out.write (conent); } Catch(Exception e) {e.printstacktrace (); } finally { Try{out.close (); } Catch(IOException e) {e.printstacktrace (); } } } Public Static voidmethod2 (String fileName, string content) {Try { //open a write file, the second parameter in the constructor true indicates that the file is written in append formFileWriter writer =NewFileWriter (FileName,true); Writer.write (content); Writer.close (); } Catch(IOException e) {e.printstacktrace (); } } Public Static voidmethod3 (String fileName, string content) {Try { //open a random Access file stream, read and writeRandomaccessfile Randomfile =NewRandomaccessfile (FileName, "RW"); //file length, number of bytes LongFilelength =randomfile.length (); //moves the write file pointer to the end of the file. Randomfile.seek (filelength); Randomfile.writebytes (content); Randomfile.close (); } Catch(IOException e) {e.printstacktrace (); } } Public Static voidMain (string[] args) {System.out.println ("Start"); Method1 ("C:/test.txt", "Append to end of file"); System.out.println ("End"); }
Java Append file