的FileStream追加数据顶部 [英] FileStream Append Data at the top

查看:138
本文介绍了的FileStream追加数据顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的工具。

欲在该文件的顶部追加数据,但它不overwritting追加。

I want to append data at the top of the file, but it is overwritting not appending.

有关,例如:考虑file.txt的:

For eg : Consider the file.txt :

现有的东西在这里

现在我想追加更多的东西存在这里当前行之前。有没有一种方法,我可以不需要使用2个文件流?

Now I want to append "Something more existing here" before the current line. Is there a way I can do without using 2 FileStreams ?

推荐答案

没有。文件系统基本上不支持将数据插入文件 - 你要么附加在文件的结尾,或覆盖现有数据。至少,我不知道的的任何文件系统不的支持这一点,和标准的API没有。

No. File systems basically don't support inserting data into the file - you either append at the end of the file, or overwrite existing data. At least, I don't know of any file system that does support this, and the standard APIs don't.

要改变任何其他方式的文件,最好的办法是写一个新的文件,从旧的文件中读取,你(写preceding文字后,你的情况)需要。然后删除旧文件并重新命名新的文件具有相同的名称作为旧的文件。

To change a file in any other way, the best way is to write a new file, reading from the old file where you need to (in your case after writing the preceding text). Then delete the old file and rename the new file to have the same name as the old file.

(这方面的一个更安全的版本,包括重新命名旧文件,然后重命名新的文件,然后删除旧文件 - 这允许恢复如果有错误)

(A safer version of this involves renaming the old file, then renaming the new file, then deleting the old file - this allows for recovery if anything goes wrong.)

这篇关于的FileStream追加数据顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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