Java append content to txt file

 Append the text content to be recorded to the txt file line by line

/**
 * @param fileName 文件名(绝对路径)
 * @param content 追加内容
 */
public void writeError(String fileName, String content) {
    try {
        // 打开一个随机访问文件流,按读写方式
        RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
        // 文件长度,字节数
        long fileLength = randomFile.length();
        // 将写文件指针移到文件尾
        randomFile.seek(fileLength);
        randomFile.writeBytes(content+"\r\n");
        randomFile.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

 

Guess you like

Origin blog.csdn.net/y_bccl27/article/details/114435985