Here you can find the source of appendToFile(String filename, String content, int line)
public static void appendToFile(String filename, String content, int line)
//package com.java2s; //License from project: Apache License import java.io.IOException; import java.io.RandomAccessFile; public class Main { public static void appendToFile(String filename, String content, int line) { try {//w w w.j av a 2 s . co m RandomAccessFile randomFile = new RandomAccessFile(filename, "rw"); long fileLength = randomFile.length(); randomFile.seek(fileLength); long end = randomFile.getFilePointer(); int j = 0; long a = 0; while ((end >= 0) && (j <= 2)) { end--; randomFile.seek(end); byte n = randomFile.readByte(); if (n == '\n') { a = randomFile.getFilePointer(); j++; if (j == line) { break; } } } randomFile.seek(a); randomFile.writeBytes(content); randomFile.close(); } catch (IOException e) { e.printStackTrace(); } } }