数据持久化存储--指定文件路径

//写文件到本地

//创建文件路径。将内容写进创建的文件中

NSArray *arrayTest = @[@"1",@"2",@"3",@"4"];

//找到文件路径

NSString * filePath1 = [self getFilePath];

//写入文件到filePath下

[arrayTest writeToFile:filePath1 atomically:YES];

//从文件中读取

//静态

NSArray * arr = [NSArray arrayWithContentsOfFile:filePath1];

//动态

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;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容