//寫文件到本地
//創(chuàng)建文件路徑靴患。將內(nèi)容寫進(jìn)創(chuàng)建的文件中
NSArray *arrayTest = @[@"1",@"2",@"3",@"4"];
//找到文件路徑
NSString * filePath1 = [self getFilePath];
//寫入文件到filePath下
[arrayTest writeToFile:filePath1 atomically:YES];
//從文件中讀取
//靜態(tài)
NSArray * arr = [NSArray arrayWithContentsOfFile:filePath1];
//動態(tài)
NSArray * arr1 = [[NSArray alloc] initWithContentsOfFile:filePath1];
NSLog(@"%@",arr1);
//字典存儲和讀取
NSDictionary *dictionary = @{@"1":@"L",@"2":@"O",@"3":@"V",@"4":@"E"};
//存儲getDicPath寫文件夾
NSString *filePath2 = [self getDicPath];
[dictionary writeToFile:filePath2 atomically:YES];
//讀取
NSDictionary *dd = [NSDictionary dictionaryWithContentsOfFile:filePath2];
NSLog(@"%@",dd);
}
- (NSString *)getFilePath {//文件路徑
//1.documents文件
NSString *homePath =NSHomeDirectory();
//追加字符串名字不能寫錯XXX.plist
NSString *filePath = [homePath stringByAppendingPathComponent:@"Documents/arrayTest.plist"];
//追加path/
//NSString *filePath1 =?[homePath stringByAppendingString:@"/Documents"];
//2.documents文件
//NSDocumentDirectory Document目錄固定
//NSUserDomainMask用戶域名固定
//NSString * documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSLog(@"%@",filePath);
NSString * library = NSSearchPathForDirectoriesInDomains( NSLibraryDirectory, NSUserDomainMask,YES)[0];
NSLog(@"%@",library);
//NSString * temp = NSSearchPathForDirectoriesInDomains(NSTemporaryDirectory, NSUserDomainMask, YES)[0];沒有
return filePath;
}
- (NSString *)getDicPath {
NSString * homePath =NSHomeDirectory();
//字典
NSString * DicPath = [homePath stringByAppendingPathComponent:@"Library/dictionary.plist"];
return DicPath;
}