我可以在iphone上写一个文本文件..但每次我写的我的前一个值都被删除了,有没有办法继续写数据\n?
这是我的代码
NSString *cc=@"1";
[cc writeToFile:storePath atomically:YES];
NSString *myText1 = [NSString stringWithContentsOfFile:storePath];
NSLog(@"text is %@",myText1);
Run Code Online (Sandbox Code Playgroud)
这给了我1现在我想添加2像1然后2
使用NSFileHandle方法seekToEndOfFile和调用writeData
NSFileHandle *aFileHandle;
NSString *aFile;
aFile = [NSString stringWithString:@"Your File Path"]; //setting the file to write to
aFileHandle = [NSFileHandle fileHandleForWritingAtPath:aFile]; //telling aFilehandle what file write to
[aFileHandle truncateFileAtOffset:[aFileHandle seekToEndOfFile]]; //setting aFileHandle to write at the end of the file
[aFileHandle writeData:[toBeWritten dataUsingEncoding:nil]]; //actually write the data
Run Code Online (Sandbox Code Playgroud)