我正在为 Mac 开发 Cocoa 应用程序。我必须将文件的数据附加到新行中的现有文件中。我试图通过以下代码来做到这一点:
NSData * theData = [NSData dataWithContentsOfFile: @"~/Desktop/test/new.rtf"
options: NSMappedRead
error: &error];
NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:@"~/Desktop/test/test.rtf"];
[output seekToEndOfFile];
[output writeData:theData];
但是这段代码不起作用。这段代码什么也没做。既不给出任何错误,也不将文件 new.rtf 的数据写入 test.rtf。知道如何将文件 new.rtf 的数据附加到新行的 test.rtf 中吗?
最佳答案
NSString *readFile = [@"~/Desktop/test/new.rtf" stringByExpandingTildeInPath];
NSString *writeFile = [@"~/Desktop/test/test.rtf" stringByExpandingTildeInPath];
NSData * theData = [NSData dataWithContentsOfFile:readFile
options:NSMappedRead
error:NULL];
NSFileHandle *output = [NSFileHandle fileHandleForUpdatingAtPath:writeFile];
[output seekToEndOfFile];
[output writeData:theData];
[output closeFile];
关于cocoa - 将数据附加到新行 cocoa 中的现有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9768256/