如何使用FileOutputStream删除.txt文件中的行? [英] How to delete a line in a .txt file using FileOutputStream?
问题描述
我需要从存储在android设备内部存储器中的 .txt
文件中删除一行.我正在使用
I need to delete a line from a .txt
file stored in the android device's internal storage. I am using
FileOutputStream fos = context.openFileOutput("file.txt",Context.MODE_APPEND);
打开 .txt
文件.我已经知道要删除的行.怎么删除呢?抱歉,如果这个问题很简单,我看了一下,却没有在Google上找到答案.
to open the .txt
file. I already know the line which is to be deleted. How to delete it? Sorry if this question is easy, I looked and couldn't find an answer on Google.
推荐答案
您无法轻松地从文件中删除一行文本.您可以用包含相同数量字符的其他数据覆盖它,但是基本上文件不支持从文件中的任意位置删除数据.
You can't easily remove a line of text from a file. You can overwrite it with other data which contains the same number of characters, but basically files don't support removing data from arbitrary locations in a file.
您可能想要:
- 为现有文件创建阅读器
- 为新文件创建作者
- 一次复制一行,跳过要删除的行
- 关闭读者和作者
- 不重命名旧文件
- 将新文件重命名为旧文件名
- 删除重命名的旧文件
(这些步骤可确保即使出现问题,也不会丢失数据.)
(Those steps make sure that even if something goes wrong, you never lose data.)
这篇关于如何使用FileOutputStream删除.txt文件中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!