我正在为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?
答案 0 :(得分:3)
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];