如何在iPhone上进行文件读/写操作?这是我需要指定保存文件的路径?如何在iPhone中获取当前的工作目录?
谢谢
答案 0 :(得分:7)
写入文件:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *myFile = [documentsDirectory stringByAppendingPathComponent:@"myFile"];
[data writeToFile:myFile atomically:YES];
阅读文件:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"txt"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
if (myData) {
// do something useful
}