您的位置:首页 > 编程语言 > Java开发

Java中使用RandomAccessFile向文件最后写入内容

2013-04-14 22:33 701 查看
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的方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: