在iPhone中读写文件

时间:2010-09-07 03:28:48

标签: iphone filesystems

如何在iPhone上进行文件读/写操作?这是我需要指定保存文件的路径?如何在iPhone中获取当前的工作目录?

谢谢

1 个答案:

答案 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  
}