用Java删除文件的最后一行 [英] Deleting the last line of a file with Java

查看:668
本文介绍了用Java删除文件的最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .txt 文件,我想用Java处理。我想删除它的最后一行。

我不需要将整个内容复制到另一个文件而忽略最后一行,我需要如何实现这个想法。任何建议?

解决方案

您可以通过扫描文件找到最后一行的开头,然后使用FileChannel.truncate 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.

这篇关于用Java删除文件的最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆