写入文件而不覆盖ObjC

时间:2021-09-07 20:26:46

I would like to write text to a file, but when searching for solution, I find "read-append-write" everywhere, but the file is too large for the memory of an iOS device, and it freezes, and resprings. Is there any other solution to do it?

我想将文本写到文件中,但是在搜索解决方案时,我发现到处都是“读-追加-写”,但是这个文件对于iOS设备的内存来说太大了,它会死机并重新生成。还有别的办法吗?

1 个解决方案

#1


10  

You can use the NSFileHandle class in order not to have to read the whole file into memory (which is, by the way, bad practice for any file!):

您可以使用NSFileHandle类,以便不必将整个文件读入内存(顺便说一句,这对任何文件都是不好的做法!)

NSFileHandle *fh = [NSFileHandle fileHandleForWritingAtPath:@"/path/to/file.ext"];
[fh seekToEndOfFile];

NSData *data = // obtain an NSData somehow
[fh writeData:data];
[fh closeFile];

#1


10  

You can use the NSFileHandle class in order not to have to read the whole file into memory (which is, by the way, bad practice for any file!):

您可以使用NSFileHandle类,以便不必将整个文件读入内存(顺便说一句,这对任何文件都是不好的做法!)

NSFileHandle *fh = [NSFileHandle fileHandleForWritingAtPath:@"/path/to/file.ext"];
[fh seekToEndOfFile];

NSData *data = // obtain an NSData somehow
[fh writeData:data];
[fh closeFile];