重要概念:某些路徑下「只能讀,不能寫」的緣由html
iPhone、ipad真機上ios
Resouces文件夾:是只讀的,沒法寫入。xcode
document 和temp文件夾:可讀,可寫。函數
1、工程結構atom
2、源代碼spa
一、頭文件:PlistManage.hcode
@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
二、一些基本函數的實現:htm
//獲取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; }
三、添加:包括建立不存在的空文件blog
-(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(@"添加內容完成!!"); }
運行結果:ip
對應路徑下生成了新文件:
內容以下:
四、讀取
-(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"]); }
運行結果:
五、修改
-(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); }
運行結果:
六、刪除
-(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); }
3、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