码迷,mamicode.com
首页 > 其他好文 > 详细

关于Plist文件的操作

时间:2014-07-25 13:51:52      阅读:332      评论:0      收藏:0      [点我收藏+]

标签:des   style   color   os   文件   io   width   for   

重要概念:某些路径下“只能读,不能写”的原因

  iPhone、ipad真机上

  Resouces文件夹:是只读的,无法写入。

  document 和temp文件夹:可读,可写。

@inter PlistMange :NSObject

-(void)resourcePathFileRead; //当前工程资源目录,不同于真机“沙箱”中的路径

-(NSString*)docPath; //获取document文件夹路径

-(BOOL)isDirNeedCreate:(NSString*)dirPath; //判断目录是否需要新的创建

-(BOOL)isFileNeedCreate:(NSString*)filePath;// 判断文件是否需要被创建

-(void)doAdd;

-(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];//完全覆盖

bubuko.com,布布扣}

对应路径下生成了新文件:

bubuko.com,布布扣内容如下:bubuko.com,布布扣

读取

-(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"])

运行记录如下

bubuko.com,布布扣}

修改

-(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);
}bubuko.com,布布扣

各个目录的获取

-(void)resourcePathFileRead{
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"resourceData" ofType:@"plist"];
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    NSLog(@"resourceData.plist文件信息如下:\n%@", data);
}

关于Plist文件的操作,布布扣,bubuko.com

关于Plist文件的操作

标签:des   style   color   os   文件   io   width   for   

原文地址:http://www.cnblogs.com/286722723ma/p/3867532.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!