package xml.parser;
import java.io.RandomAccessFile;
public class AppendInfoToFileEnd {
/**
* 向文件最后一行写入内容
*
* @param filePath
* @param info
*/
public static void append(String filePath, String info) {
RandomAccessFile f;
try {
f = new RandomAccessFile(filePath, "rw");
long fileLength = f.length();// 获取文件的长度即字节数
// 将写文件指针移到文件尾
f.seek(fileLength);
f.write("\r\n".getBytes());
f.write(info.getBytes());
// 关闭流
f.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
为了避免写入乱码,使用了写入byte的方法。
Java中使用RandomAccessFile向文件最后写入内容
最新推荐文章于 2024-07-15 22:55:18 发布