覆盖文件中的当前内容 [英] Overwriting current contents in file

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

问题描述

我想覆盖名为 data.txt 的文本文件开头的当前数据(在我的资源文件夹中).总是只有一行(没有空格或任何东西),我希望在适当的时候覆盖这一行.这就是我到目前为止一直在做的事情,但这只是在文件末尾写入内容,而不是从头开始,也就是覆盖我需要的行.如果有人可以帮助我,将不胜感激.谢谢!

I want to overwrite the current data at the beginning of the my text file called data.txt (in my resources folder). There will always be only one line (no spaces or anything) and I want this line to be overwritten when appropriate. This is how I have been doing it so far but this only writes contents at the end of the file and doesn't begin from the beginning aka overwriting the line I need. If anyone can help me it would be appreciated. Thanks!

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0];

//append filename to docs directory
NSString *myPath =  [documentsDirectory stringByAppendingPathComponent:@"data.txt"];
fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:myPath];

writtenString= [[NSString alloc]initWithFormat:@"%d", message];     

[fileHandle seekToEndOfFile];
[fileHandle writeData:[writtenString dataUsingEncoding:NSUTF8StringEncoding]];

[writtenString release];

推荐答案

Mike 的回答将允许您覆盖文件的内容,但不会以您想象的面向行的方式覆盖.

Mike's answer will allow you to overwrite the contents of your file, but not in a nice line-oriented manner that you're imagining.

你不能调整文件的内部内容,这样,如果你插入东西,现有的后续数据将被推后,如果你删除东西,现有的后续数据将在之后被拉出移除点.

You can't adjust the internal contents of a file such that, if you insert stuff, the existing following data will be pushed later and, if you remove stuff, the existing following data will be pulled up to come right after the removal point.

您有两个基本操作可用:您可以截断某个长度(可能为 0)的文件,丢弃该点之后的所有旧数据.并且可以在某个位置写入数据.如果该位置已经有数据,则将其覆盖(即丢失).如果您写入的字节范围超出文件末尾,则文件将被扩展.

You have two basic operations available: you can truncate a file at a certain length, possibly 0, discarding any old data past that point. And you can write data at a certain position. If there was already data at that position, it is overwritten (i.e. lost). If the byte range to which you're writing extends past the end of the file, the file is extended.

但是,您既没有截断也没有覆盖的任何旧数据仍然保留在原处.在任何情况下都不会移动.

However, any of the old data which you neither truncated nor overwrote remains where it is. In no case is it moved.

因此,如果您想更改文本文件的第一行,并且您不能确定新行的长度与旧行的长度完全相同,那么您必须注意将所有内容从第二行到它的新位置.一种适用于小文件的简单方法是将整个内容读入内存,在那里进行调整,然后再将其全部写出.另一种方法是遍历文件,分块读取,然后将它们写到新位置.如果您将数据移向起点,则从头开始;如果您将数据移到最后,则从最后开始.

So, if you want to change the first line of a text file, and if you can't be sure that the new line is exactly the same length as the old line, then you have to take care of moving everything from the second line on to its new location. One simple approach, suitable for small files, is to read the entire thing into memory, adjust it there, and write it all out again. Another is to iterate over the file, reading in chunks and then writing them out in their new locations. If you are moving data toward the start, you start at the beginning; if you are moving data toward the end, you start at the end.

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

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