1.文本内容逐行写入到txt文件中
import java.io.RandomAccessFile;
/**
* 功能:将内容逐行写入到txt文件中
* @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) {
log.error("将内容写入到txt文件中出现异常:",e);
}
}
2.从txt文件中逐行读取文本内容
import java.io.RandomAccessFile;
/**
* 功能:从txt文件中逐行读取内容
* @param txtPath txt文件的路径
*/
public static vo