void appendContent(String fileName, String content) {
RandomAccessFile randomFile = null;
try {
// 打开一个随机访问文件流,按读写方式
randomFile = new RandomAccessFile(fileName, "rw");
// 文件长度,字节数
long fileLength = randomFile.length();
// 将写文件指针移到文件尾。
randomFile.seek(fileLength);
randomFile.writeBytes(content);
} catch (IOException e) {
e.printStackTrace();
} finally{
if(randomFile != null){
try {
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Java中向指定txt文件追加内容
最新推荐文章于 2025-03-03 00:15:00 发布
本文介绍了一种使用Java的RandomAccessFile类实现文件内容追加的方法。通过打开一个随机访问文件流,并将写文件指针移到文件尾部,可以在不删除原有内容的基础上添加新的文本内容。
6888

被折叠的 条评论
为什么被折叠?



