使用RandomAccessFile向文件尾端追加内容
时间:2021-07-01 10:21:17
帮助过:2人阅读
public void BufferReaderTest()
throws FileNotFoundException{
//System.in为字节流,需要使用InputStreamReader转成字符流
InputStreamReader isr =
new InputStreamReader(System.in);
//把普通Reader包装成BufferedReader(具有缓冲功能,能按行读取)
BufferedReader br =
new BufferedReader(isr);
//使用构造器RandomAccessFile,文件和文件的访问模式
RandomAccessFile raf =
new RandomAccessFile(
new File("D:/RUNNING_COPY.txt"), "rw"
);
String line =
null;
try {
//循环逐行读取
while((line = br.readLine())!=
null){
if (line.equals("exit"
)) {
System.exit(1
);
}
//记录指正移动到最后
raf.seek(raf.length());
//写入内容
raf.write((line+"\r\n"
).getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
br.close();
isr.close();
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用RandomAccessFile向文件尾端追加内容
标签: