将数据写入文件而不删除iPhone中的旧内容
我想在不替换旧内容的情况下将数据写入.txt文件。我已将该文件从NSMainBundle移动到documents目录。我可以使用代码写入该文件将数据写入文件而不删除iPhone中的旧内容,iphone,file,Iphone,File,我想在不替换旧内容的情况下将数据写入.txt文件。我已将该文件从NSMainBundle移动到documents目录。我可以使用代码写入该文件 NSData *data=[NSKeyedArchiver archivedDataWithRootObject:recentMainArray]; NSFileHandle *myHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath]; [myHandle seekToEndOfFile
NSData *data=[NSKeyedArchiver archivedDataWithRootObject:recentMainArray];
NSFileHandle *myHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
[myHandle seekToEndOfFile];
[myHandle writeData:data];
[myHandle closeFile];
但是当我试图显示文件的内容时,我在该路径中没有任何数据。文件也存在于该路径中
NSFileManager *manager = [NSFileManager defaultManager];
//文件路径->文档目录文件路径
“null”,作为这两个NSLog的结果打印
请任何人让我知道这个问题的解决方案。仅供参考,我正在使用模拟器进行测试,这是否会产生问题。请向我建议解决方案。我搜索了很多,但找不到解决方案
提前感谢大家将数据读入一些可变结构(字典、数组、字符串),然后将新数据附加到同一个字符串中。现在将数据写入同一路径。因此,新附加的数据将写入文件中
if([manager fileExistsAtPath:filePath]) {
NSMutableArray *savedArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
NSMutableArray *storedRecentID = [savedArray objectAtIndex:0];
NSMutableArray *storedRecentName = [savedArray objectAtIndex:1];
NSLog(@"ID:%@",[savedArray objectAtIndex:0]);
NSLog(@"Name:%@",[savedArray objectAtIndex:1]);
}
else {
NSLog(@"file not found, save something to create the file");
}