且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用Java删除文件的最后一行

更新时间:2023-12-03 13:11:34

您可以通过扫描文件找到最后一行的开头,然后使用FileChannel.truncate$c$ c> RandomAccessFile.setLength


I have a .txt file, which I want to process in Java. I want to delete its last line.

I need ideas on how to achieve this without having to copy the entire content into another file and ignoring the last line. Any suggestions?

You could find the beginning of the last line by scanning the file and then truncate it using FileChannel.truncate or RandomAccessFile.setLength.