您的位置:首页 > 移动开发 > IOS开发

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);

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 沙盒 存储 json