NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"xxx.txt"]; https://www.cnblogs.com/FBiOSBlog/p/5819418.html https://blog.csd ...
首先,先總結一下如何獲取Documents目錄,在ios開發中,我們經常需要檢索Documents目錄的完整路徑以便讀取和寫入文件,我總結了以下兩種方法: 1、NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 2、NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; 以上documentsDirectory就是獲取的Documents的完整路徑。 程式的所有資源文件,存儲在程式包中,獲取程式包路徑的方法是: NSString *appDirectory = [[NSBundle mainBundle] bundlePath]; 在實際開發中我們有時需要判斷Documents下的某個資源文件是否存在,如果不存在,則從程式包中拷貝進去,現在假設要判定的文件是"xxx.txt",判定代碼如下: NSFileManager *fileManager = [NSFileManager defaultManager]; //NSString *filePath = [self [documentsDirectory stringByAppendingPathComponent:@"xxx.txt"]];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"xxx.txt"];
if(![fileManager fileExistsAtPath:filePath]) //如果不存在 { NSLog(@"xxx.txt is not exist"); NSString *dataPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/xxx.txt"];//獲取程式包中相應文件的路徑 NSError *error; if([fileManager copyItemAtPath:dataPath toPath:filePath error:&error]) //拷貝 { NSLog(@"copy xxx.txt success"); } else { NSLog(@"%@",error); } }
https://www.cnblogs.com/FBiOSBlog/p/5819418.html
https://blog.csdn.net/csdn_hhg/article/details/80458646
https://www.jianshu.com/p/2d0ad82c59fa//讀寫