从.DAT文件中删除内容 [英] Deleting content from a .DAT file

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

问题描述

我正在尝试使用Stream Writer在vb.net中创建累积数据文件。我遇到了无法删除文件中当前文本以替换它的问题。如果可能的话,我怎么能这样做。



我尝试了什么:



到目前为止,我已经尝试将光标设置在插入点并写入空格。只是积累了更多。

解决方案

文本文件没有插入或删除功能:所有你能做的就是读写 - 当你写的时候您要么覆盖现有数据,要么让文件更长。

如果要插入数据,可以创建新文件,复制到要插入数据的位置,添加新数据,然后复制文件的其余部分。

如果要从文本文件中删除数据,可以执行类似的操作:创建新文件,复制到要删除的数据,然后跳过它并复制文件的其余部分。



使用StreamWriter访问的任何文件都会遇到同样的问题。



实际上,所有文件以相同的方式操作:但是一些(如数据库)组织文件内的数据,以便不是实际删除信息,而是从已使用列表中删除记录并将其传输到免费列表。这样,它不必为每次更改复制整个文件。



如果您想要的是每次应用程序运行时删除文件内容,那么最简单的方法是在应用程序启动过程中逐字删除该文件: File.Delete Method(String)(System.IO) [ ^ ]将执行此操作,或者您可以在启动时使用FileMode.Create选项将其打开: FileMode枚举(System.IO) [ ^ ]

I am trying to create an accumulating data file in vb.net using a Stream Writer. I have run into the problem of not being able to delete the text currently on the file to replace it. How, if possible, could I do this.

What I have tried:

So far I have tried setting the cursor at the insert point and writing null spaces. That just accumulate more.

解决方案

Text files don't have "insert" or "delete" functions: all you can do is read and write - and when you write you either overwrite existing data or make the file longer.
If you want to insert data, you create a new file, copy up to the point at which you want to insert the data, add your new data, then copy the rest of the file.
If you want to remove data from a text file, you do something similar: you create a new file, copy up to the data you want to delete, then skip that and copy the rest of the file.

Any file you access with a StreamWriter will have the same problems.

In fact, all files operate the same way: but some (like databases) organise the data inside the file so that instead of actually deleting the information, it removes the record from the "used" list and transfers it to the "free" list. That way, it doesn't have to copy the whole file for every change.

If what you want is to delete the file content every time your app runs, then the simplest way is to literally delete the file as part of your application startup: the File.Delete Method (String) (System.IO)[^] will do it, or you can open it with the FileMode.Create option when you start: FileMode Enumeration (System.IO)[^]


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

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