pragma mark - 沙盒保存文件
+ (void)createDocumentFileName:(NSString *)fileName content:(NSDictionary *)contentDic
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *directory = [[NSString alloc]initWithFormat:@"%@/%@",documentsDirectory,fileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
// 创建目录
if (![fileManager fileExistsAtPath:directory])
{
[fileManager createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString *filePath = [directory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",fileName]];
NSLog(@"filePath %@",filePath);
//写文件
[contentDic writeToFile:filePath atomically:YES];
}
+ (NSDictionary *)getDocumentFileName:(NSString *)fileName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *directory = [[NSString alloc]initWithFormat:@"%@/%@",documentsDirectory,fileName];
NSString *filePath = [directory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",fileName]];
NSDictionary *resultDic = [NSDictionary dictionaryWithContentsOfFile:filePath];
NSLog(@"resultdic.....%@",resultDic);
return resultDic;
}
+ (void)deleteDocumentFileName:(NSString *)fileName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *directory = [[NSString alloc]initWithFormat:@"%@/%@",documentsDirectory,fileName];
NSString *filePath = [directory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",fileName]];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:filePath error:nil];
}
网友评论