
重要概念:某些路径下“只能读,不能写”的原因
iphone、ipad真机上
Resouces文件夹:是只读的,无法写入。
document 和temp文件夹:可读,可写。
一、工程结构
二、源代码
1、头文件:PlistManage.h
@interface PlistManage : NSObject -(void)resourcePathFileRead;//当前工程资源目录,不同于真机“沙箱”中的路径 -(NSString *)docPath;//获取document文件夹路径 -(BOOL)isDirNeedCreate:(NSString *)dirPath;//判断目录是否需要新创建 -(BOOL)isFileNeedCreate:(NSString *)filePath;//判断文件是否需要创建
-(void) doAdd; -(void) doRead; -(void) doModify; -(void) doDelete; @end
2、一些基本函数的实现:
//获取document目录路径
-(NSString *)docPath
{
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [paths objectAtIndex:0];
}
//路径是否需要创建
-(BOOL)isDirNeedCreate:(NSString *)dirPath
{
if ( NO == [[NSFileManager defaultManager] fileExistsAtPath:dirPath] )
{
return [[NSFileManager defaultManager] createDirectoryAtPath:dirPath
withIntermediateDirectories:YES
attributes:nil
error:NULL];
}
return NO;
}
//文件是否需要创建
-(BOOL)isFileNeedCreate:(NSString *)filePath{
if ( NO == [[NSFileManager defaultManager] fileExistsAtPath:filePath] )
{
return [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
}
return NO;
}
3、添加:包括创建不存在的空文件
-(void) doAdd{
NSString *docPath=[self docPath];
NSLog(@"当前docment路径:\n%@",docPath);
NSString *dataFile=[docPath stringByAppendingPathComponent:@"docData.plist"];
if (YES==[self isFileNeedCreate:dataFile]) {
NSLog(@"文件原先不存在,现已新建空文件!");
}else{
NSLog(@"文件已存在,无需创建!");
}
NSMutableDictionary *plistDic = [[NSMutableDictionary alloc ] init];
// 添加2个“单条记录”
[plistDic setObject:@"shanghai" forKey:@"recordKey001"];
[plistDic setObject:@"beijing" forKey:@"recordKey002"];
// 添加2个“字典记录”
[plistDic setObject:[NSDictionary dictionaryWithObjectsAndKeys:@"Jack",@"name",@"22",@"age",nil] forKey:@"dicKey001"];
[plistDic setObject:[NSDictionary dictionaryWithObjectsAndKeys:@"Tom",@"name",@"33",@"age",nil] forKey:@"dicKey002"];
[plistDic writeToFile:dataFile atomically:YES];//完全覆盖
NSLog(@"添加内容完成!!");
}
运行结果:
对应路径下生成了新文件:
内容如下:
4、读取
-(void) doRead{
NSString *dataFile=[[self docPath] stringByAppendingPathComponent:@"docData.plist"];
//读取所有内容
NSDictionary* dic = [NSDictionary dictionaryWithContentsOfFile:dataFile];
NSLog(@"完整内容:\n%@",dic);
//读取第一层“字典记录”
NSDictionary* dicValue=[dic objectForKey:@"dicKey001"];
NSLog(@"读取第一层“字典记录”:\n%@",dicValue);
//读取第一层“字典记录”中的“子元素”
NSLog(@"读取第一层“字典记录”中的“子元素”:\nname=%@",[dicValue objectForKey:@"name" ]);
//读取第一层“单条记录”
NSLog(@"读取第一层“单条记录”:\nrecordKey001=%@",[dic objectForKey:@"recordKey001"]);
}
运行结果:
5、修改
-(void) doModify{
NSString *dataFile=[[self docPath] stringByAppendingPathComponent:@"docData.plist"];
NSMutableDictionary *dic = [[[NSMutableDictionary alloc]initWithContentsOfFile:dataFile]mutableCopy];
//修改“单条记录”
NSString *city = [dic objectForKey:@"recordKey001"];
city = @"shanghai-new";
[dic setObject:city forKey:@"recordKey001"];
//修改“字典记录”
NSMutableDictionary *personInfo = [dic objectForKey:@"dicKey001"];
NSString *name = [dic objectForKey:@"name"];
name = @"Jack-new";
[personInfo setValue:name forKey:@"name"];
[dic setValue:personInfo forKey:@"dicKey001"];
//写入文件
[dic writeToFile:dataFile atomically:YES];
NSDictionary* dicResult = [NSDictionary dictionaryWithContentsOfFile:dataFile];
NSLog(@"修改结果:\n%@",dicResult);
}
运行结果:
6、删除
-(void) doDelete{
NSString *dataFile=[[self docPath] stringByAppendingPathComponent:@"docData.plist"];
NSMutableDictionary *dic = [[[NSMutableDictionary alloc]initWithContentsOfFile:dataFile]mutableCopy];
//删除“单条记录”
[dic removeObjectForKey:@"recordKey001"];
[dic removeObjectForKey:@"dicKey001"];
//删除“字典记录”
//写入文件
[dic writeToFile:dataFile atomically:YES];
NSDictionary* dicResult = [NSDictionary dictionaryWithContentsOfFile:dataFile];
NSLog(@"修改结果:\n%@",dicResult);
}
运行结果:
各个目录的获取:
http://www.cnblogs.com/ios-wmm/p/3299695.html
如果是操作 Resource下的plist文件,可由以下代码完成:只能读,不能写
-(void)resourcePathFileRead{
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"resourceData" ofType:@"plist"];
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
NSLog(@"resourceData.plist文件信息如下:\n%@", data);
}
三、ios程序的“沙箱原理”
xcode6中的模拟器位置:
/Users/wuxiaofeng/资源库/Developer/
3个基本目录:
原理:
a)iTunes在与iPhone同步时,备份所有的Documents和Library文件。
b)iPhone在重启时,会丢弃所有的tmp文件。
参考:沙箱,路径获取,文件操作
http://www.cnblogs.com/dyllove98/archive/2013/07/30/3225955.html