从java文件中删除一行 [英] Delete a line from a file in java

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

问题描述

好的,所以我试图用java删除文本文件中的行。目前我这样做的方式是跟踪行号和输入索引。索引是我想要删除的行。所以每次我读一行新的数据时,我都会增加行数。现在,当我达到的行数是相同的索引,我不写数据到临时文件。现在这个工作,但是如果我正在处理大文件,我不得不担心内存限制。我怎样才能做到这一点..文件标记?例如..将文件标记放在我想要删除的行上。那么删除那一行?或者这只是太多的工作?

解决方案

不要将文件保存在内存中,只需一次读取一行并将其写入临时文件一行一行跳过需要删除的行。

Ok, so I'm trying to delete lines from a text file with java. Currently the way I'm doing this, is I'm keep track of a line number and inputting an index. The index is the line I want deleted. So each time I read a new line of data I increment the line count. Now when I reach the line count that is the same index, I dont write the data to the temporary file. Now this works, but what if for example I'm working with huge files and I have to worry about memory restraints. How can I do this with.. file markers? For example.. place the file marker on the line I want to do delete. Then delete that line? Or is that just too much work?

解决方案

Don't keep the file in memory, just read it one line at a time and write it out to the temporary file one line at at a time skipping the line that needs to be deleted.

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

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