Package cn.com; import java. io. fileWriter; import java. io. randomAccessFile; // Problem description: // Append content to the end of the file // Method 1: use the RandomAccessFile class // 1 to set the randomAccessFile mode to rw // 2 to move randomAccessFile (seek) to the end of the file // 3 to append data // 4 to close the stream // Method 2: use the FileWriter class // 1 to set the second parameter of the FileWriter constructor method to true. append/2 to the end to append data // 3 close the stream public class FileTest {public static void main (String [] args) {FileTest fileTest = new FileTest (); fileTest. addContentFirst ("F: \ temp.txt", "test1"); fileTest. addContentSecond ("F: \ temp.txt", "test2");} public void addContentFirst (String filePath, String newContent) {try {RandomAccessFile randomAccessFile = new RandomAccessFile (filePath, "rw"); long fileLength = randomAccessFile. length (); randomAccessFile. seek (fileLength); randomAccessFile. write (newContent. getBytes ("UTF-8"); randomAccessFile. close () ;}catch (Exception e) {}} public void addContentSecond (String filePath, String newContent) {try {FileWriter fileWriter = new FileWriter (filePath, true); fileWriter. write (newContent); fileWriter. close () ;}catch (Exception e ){}}}