iOS文件存储

在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:dicoptions:NSJSONWritingPrettyPrintederror:&err];

if(err) {

NSLog(@"%@",err);

}

else{

//==写入文件

NSLog(@"%@",[datawriteToFile:Json_pathatomically: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:dataoptions:NSJSONReadingAllowFragmentserror:&err];

if(err) {

NSLog(@"json解析失败:%@",err.userInfo);

}

else{

NSLog(@"解析成功:%@",dic);

returndic;

}

}

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;

}

returndic;

}

//删除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]];

BOOLblHave=[[NSFileManagerdefaultManager]fileExistsAtPath:Json_path];

if(!blHave) {

NSLog(@"文件不存在:%@",fileName);

}

else{

NSError* err;

BOOLblDele= [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 =[fileManagercontentsOfDirectoryAtPath:documenterror:NULL];

for(NSString*fileinfileList) {

NSLog(@"%@",file);

}

}

你可能感兴趣的:(iOS文件存储)