首页 > 其他 > 详细

oc 文件读写操作

时间:2015-04-30 17:27:44      阅读:308      评论:0      收藏:0      [点我收藏+]

 

  NSHomeDirectory(); //获取当前路径

  stringByAppendingPathComponent:@"/file1" 新建文件的路径

 

  NSFileManager: 文件管理类

  [NSFileManager defaultManager]; //初始化 在文件操作中:用defaultmanager 代替alloc]init];

 fileManager fileExistsAtPath:filePath  判断路径中是否已经存在该名字文件

 fileManager createFileAtPath:filePath contents:nil attributes:nil 创建文件

 

 

 native类写入文件:

  1、写入文件方法一:

 dataUsingEncoding:NSUTF8StringEncoding];// 对象方法  把内容转化成data

 NSFileHandle fileHandleForWritingAtPath:filePath];//类方法  准备写入-打开文件

 [fileHandle writeData:strData];//对象方法写数据

 [fileHandle closeFile];//对象方法关闭文件

 

  2、写入文件方法二:

 writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];

 

 非native类写入文件:

 NSData *data = [NSKeyedArchiver archivedDataWithRootObject:per]; //对象转化为数据

 writeToFile:@"file3" atomically:YES  //对象方法 将数据写入文件

          读取文件:

 NSData *perData = [NSData dataWithContentsOfFile:@"file3"]; //读取文件数据

 Person *per1 = [NSKeyedUnarchiver unarchiveObjectWithData:perData]; //数据转化为对象并存储到per1

oc 文件读写操作

原文:http://www.cnblogs.com/song-kl/p/4469110.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!