iOS沙盒文件存储
2016-12-09 16:43
543 查看
在h文件中:
//将json数据暂存在沙盒+(void)writeToFile:(NSDictionary*)dic WithName:(NSString*)fileName;
//读取预存的json数据
+(NSDictionary*)getDataWithFileName:(NSString*)fileName;
//将字符串转为json格式
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString;
//删除json文件
+(void)removeJsonFile:(NSString*)fileName;
//遍历沙盒下所有文件
+(void)seekAllFile;
m文件:
//将json数据暂存在文件里
+(void)writeToFile:(NSDictionary*)dic WithName:(NSString*)fileName{
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,
YES);
NSString *path=[pathsobjectAtIndex:0];
NSString *Json_path=[pathstringByAppendingPathComponent:[NSStringstringWithFormat:@"%@.json",fileName]];
NSError *err;
NSData * data=[NSJSONSerializationdataWithJSONObject:dic
options:NSJSONWritingPrettyPrintederror:&err];
if (err) {
NSLog(@"%@",err);
}
else{
//==写入文件
NSLog(@"%@",[datawriteToFile:Json_path
atomically:YES] ?@"Succeed":@"Failed");
}
}
//读取预存的json数据
+(NSDictionary*)getDataWithFileName:(NSString*)fileName
{
//读取Json
//==Json文件路径
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,
YES);
NSString *path=[pathsobjectAtIndex:0];
NSString *Json_path=[pathstringByAppendingPathComponent:[NSStringstringWithFormat:@"%@.json",fileName]];
NSData * data=[[NSDataalloc]initWithContentsOfFile:Json_path];
if (data) {
NSError *err;
NSDictionary * dic=[NSJSONSerializationJSONObjectWithData:data
options:NSJSONReadingAllowFragmentserror:&err];
if(err) {
NSLog(@"json解析失败:%@",err.userInfo);
}
else{
NSLog(@"解析成功:%@",dic);
return dic;
}
}
else{
NSLog(@"该文件无数据或不存在:%@",Json_path);
}
returnnil;
}
//将字符串转为json格式
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
if (jsonString ==nil) {
returnnil;
}
NSData *jsonData = [jsonStringdataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&err];
if(err) {
NSLog(@"json解析失败:%@",err);
returnnil;
}
return dic;
}
//删除json文件
+(void)removeJsonFile:(NSString*)fileName
{
NSFileManager * fileM=[NSFileManagerdefaultManager];
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,
YES);
NSString *path=[pathsobjectAtIndex:0];
NSString *Json_path=[pathstringByAppendingPathComponent:[NSStringstringWithFormat:@"%@.json",fileName]];
BOOL blHave=[[NSFileManagerdefaultManager]
fileExistsAtPath:Json_path];
if (!blHave) {
NSLog(@"文件不存在:%@",fileName);
}
else{
NSError * err;
BOOL blDele= [fileMremoveItemAtPath:Json_patherror:&err];
if (!blDele) {
NSLog(@"文件删除失败%@",err.userInfo);
}
}
}
//遍历沙盒下所有文件
+(void)seekAllFile{
//得到沙盒文件夹下的所有文件
NSFileManager *fileManager = [NSFileManagerdefaultManager];
NSString *document=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,
YES)
objectAtIndex:0];
NSArray *fileList;
fileList =[fileManager contentsOfDirectoryAtPath:documenterror:NULL];
for (NSString *filein fileList) {
NSLog(@"%@",file);
}
}
相关文章推荐
- 微软发布令牌漏洞公告:可绕过 Chromium 沙盒执行任意代码
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 第三方推送已死
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 如何用 Linux 命令行工具解析和格式化输出 JSON
- 使用 Python 处理 JSON 格式的数据
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- JSON、XML、TOML、CSON、YAML 大比拼
- XML 与 JSON 优劣对比
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 认识存储:块、文件和对象
- 10分钟了解MySQL5.7对原生JSON的支持与用法
- 通过更换硬盘舱位的方式解决存储问题
- Tomcat端口被占用解决方法(不用重启)
- VBA将excel数据表生成JSON文件
- “传奇”图象数据存储方式
- newtonsoft.json解析天气数据出错解决方法