As shown below:
import java.io.FileWriter;import java.io.IOException;import java.io.RandomAccessFile;/** * Append content to the end of the file. * @author haicheng.cao * */public class AppendToFile { /** * Append file to A method: use RandomAccessFile */ public static void appendMethodA(String fileName, String content) { try { // Open a random access file stream and read and write RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw"); // File length, number of bytes long fileLength = randomFile.length(); //Move the write file pointer to the end of the file. randomFile.seek(fileLength); randomFile.writeBytes(content); randomFile.close(); } catch (IOException e) { e.printStackTrace(); } } /** * Method B appends file: use FileWriter */ public static void appendMethodB(String fileName, String content) { try { //Open a filewriter, the second parameter in the constructor is true to write the file in an appended form FileWriter writer = new FileWriter(fileName, true); writer.write(content); writer.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { String fileName = "C:/temp/newTemp.txt"; String content = "new append!"; //Append file AppendToFile.appendMethodA(fileName, content); AppendToFile.appendMethodA(fileName, "append end. /n"); //Show file content ReadFromFile.readFileByLines(fileName); //Append file AppendToFile.appendMethodB(fileName, content); AppendToFile.appendMethodB(fileName, "append end. /n"); //Show file content ReadFromFile.readFileByLines(fileName); }}The above simple example of the content added by Java at the end of the file is all the content shared by the editor. I hope it can give you a reference and I hope you will support Wulin.com more.