關於Plist文件的操做

重要概念:某些路徑下「只能讀,不能寫」的緣由函數

  iPhone、ipad真機上atom

  Resouces文件夾:是隻讀的,沒法寫入。spa

  document 和temp文件夾:可讀,可寫。ip

@inter PlistMange :NSObjectci

-(void)resourcePathFileRead; //當前工程資源目錄,不一樣於真機「沙箱」中的路徑資源

-(NSString*)docPath; //獲取document文件夾路徑rem

-(BOOL)isDirNeedCreate:(NSString*)dirPath; //判斷目錄是否須要新的建立string

-(BOOL)isFileNeedCreate:(NSString*)filePath;// 判斷文件是否須要被建立it

-(void)doAdd;
io

-(void)doRead;

-(void)doModify;

-(void)doDelete;

@end

函數的實現:

-(NSString*)docPath

{

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,Yes);

  return[Paths objectIndex:0];//   返回document的路徑

}

-(BOOL)isDirNeedCreate:(NSString*)dirPath

{

  if(NO==[NSFileManager default]fileExistsAtPath:dirPath])

  {

     return [[NSFileManager defaultManager]creatDirectoryAtPath:dirPath];

   }

   return NO;

}

//文件是否須要被建立,也就是看看文件是否存在,不存在的話就建立

-(BOOL)isFileNeedCreate:(NSString*)filePath

{

  if(NO==[[NSFileManager defaultManager]fileExistsAtPath:filePath])

  {

     return [NSFileManager defaultManager]createFileAtPath:filepath contents:nil attributes:nil];

  }

    return NO;

}

添加:包括建立不存在的空文件

-(void)doAdd

{

 NSString *docPath = [self docPath];

 NSString *dataFile = [docPath stringByAppendingPathComonent:@"docData.plist"];

 if(YES==[self isFileNeedCreate:dataFike])

  {

  NSLog(@"原文件不存在,現已建立空文件");

   }else

  {

  NSLog(@"文件已經存在,無需建立!");

   }

  NSMutableDictionary *plistDic = [[NSMutableDICtionary alloc]init];

  //添加2個「單條記錄」

  [plstDic setObject:@"shanghai" forKey@"recordKey001"];

  [plstDic setObject:@"beijing" forKey@"recordKey002"];

   //添加2個「字典記錄」

  [plistDic setobject:[NSDictionary dictionaryWithObjectsAndKeys:@"Jack",@"name",@"22",@"age",nil]forKey:@"recordKey001"];

  [plistDic setobject:[NSDictionary dictionaryWithObjectsAndKeys:@"Tom",@"name",@"33",@"age",nil]forKey:@"recordKey002"];

  [plistDic writeToFile:dataFile atomically:YES];//徹底覆蓋

}

對應路徑下生成了新文件:

內容以下:

讀取

-(void)doRead{

  NSString *dataFile = [[self docPath]stringByAppendingPathComponent:@"docData.plist"];

  //讀取全部內容

  NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:dataFile];

  //獲取第一層字典

   NSDictionary *dicValue = [dic objectForKey:@"dicKey001"];

   //獲取第一層" 字典記錄"中的"子元素"

   NSlog(@"讀取第一層「字典記錄」中的「子元素」:\nname = %@ ",[dicValue objectForKey:@"name"])

   //讀取第一層「單挑記錄」

   NSlog(@"讀取第一層「 單條記錄 」:\nrecordKey001 = %@ ",[dic objectForKey:@"nrecordKey001"])

運行記錄以下

}

修改

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

各個目錄的獲取

-(void)resourcePathFileRead{
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"resourceData" ofType:@"plist"];
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    NSLog(@"resourceData.plist文件信息以下:\n%@", data);
}
相關文章
相關標籤/搜索