iOS文件写入读取

来源:互联网 发布:js 模块化编程 入门 编辑:程序博客网 时间:2024/06/07 01:55

最近在学习 iOS开发中的数据持久化里的文件读取 以下是自己的写的一点东西  还请各位看官老爷多多指点

iOS在进行数据持久化中采用沙盒, 不允许非本应用程序以外的其他应用程序访问该应用系统的一些文件.iOS8以后沙盒权限不再像以前那么严格  .

在进行本地文件读取操作中, 首先需要明确 路径 这个 词 

我们来打印一下 沙盒的路径  

 NSLog(@"%@", NSHomeDirectory());

获取后 复制  回到桌面  左上角 前往 下面的 前往文件夹 把刚才的地址粘贴上去  或者 回到桌面  cmd + shift + g 快捷方式粘贴 刚才的路径  可以看到 三个文件夹

每个沙盒下边都有三个文件夹 documents library  tmp每个路径下边都可以进行持久化

documents 最常用的持久化路径,存储一些不是太大的文件, eg.plist文件,文本文件,sqlite文件  documents上的文件将来要跟 ituns进行同步,放置太大的文件就会给苹果服务器造成太大的压力,会被拒绝.


library 把大容量的数据放置到library中的cache下边, library在开发工程中很少被直接使用,cache路径上的文件是不与itunes进行同步的.(存储一些视频音频)

Preferences这个路径上的文件也是进行同步的 ,这个文件中一般保存一些用户的设置.(也可以用来判断用户是否第一次安装该软件)


tmp 特性,这是一个临时路径,不进行同步,里边放的数据当时被重新启动时,数据就会被清空.

//如何获取document的路径

//第一种方法

NSString *documentPath1 = [NSHomeDirectory() stringByAppendingString:@"/Documents"];

NSLog(@"documentPath1 = %@", documentPath1);

//第二种方法  采用 component 

component 也是用来拼接,唯一不同 就是我们不用写 /

NSString *documentPath2 = [NSHomeDirectory() stringByAppendingPathComponent:@"DocumentPath2"];

NSLog(@"documentPath2 = %@", documentPath2);

//使用方法  获取documents的路径  用户沙盒总目录固定不变 

 NSString *getDocPath = [ NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

    NSLog(@"****%@", getDocPath);

//获取Cache的路径

NSString *getCachePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

    NSLog(@"getCachepath = %@", getCachePath);

//但是在实际开发中,一般不采用找到该路径的方法去操作,而是采用NSUserDefaults

NSUserDefaults *userDefaults= [NSUserDefaults standardUserDefaults];

//系统写好的单例

[userDefaults setObject:@"我乐意" forKey:@"不美不萌"];

  

 //获取 tmp

NSString *tmpPath = NSTemporaryDirectory();

NSLog(@"tmpPath = %@", tmpPath);


接下来,就开始进入正题,怎样 正确的对一些简单的数据进行读写操作呢  以字符串, 数组,字典为例

//字符串的读写

NSString *filePath = [documentPath stringByAppendingPathComponent:@"file.text"];

NSString *writeStr = @"不美不萌我乐意 ";

//writeStr写入文件

//想捕获错误就得先声明对象,

NSError *error = nil;

//取地址的error

BOOL writeResult = [writeStr writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];

//写成YES之后,在一些极端条件下可以起到保护作用,YES相当于把数据写到一个新的文件当中,成功之后,系统帮我们完成替换,写成NO, 相当于直接向文件中写(把之前写的数据清空)

if (writeResult == YES) {

     NSLog(@"写入成功");

} else {

     NSLog(@"error = %@", error);

}

//数组写入

NSString *arrayPath = [documentPath stringByAppendingPathComponent:@"arrayPath.plist"];

NSArray *array = @[@"shanshan",@"CYD"];

BOOL arrayResult = [array writeToFile:arrayPath atomically:YES];

if (arrayResult) {

    NSLog(@"数组写入成功");

} else{

    NSLog(@"数组写入失败");

}

//数组读取  两种方法 

//1

//获取文件的编码方式  NSStringEncoding  属于 枚举

NSStringEncoding encode = 0;

NSString *readStr = [NSString stringWithContentsOfFile:filePath usedEncoding:&encode error:nil];

NSLog(@"readStr = %@, encode = %ld", readStr, encode);

//2

NSString *readStr = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

NSLog(@"readStr = %@", readStr);

//读取数组

NSArray *readArray = [NSArray arrayWithContentsOfFile:arrayPath];

NSLog(@"readArray = %@", readArray);


//字典读写

NSDictionary *dic = @{@"name":@"bumeibumengwoleyi", @"age":@"18"};

NSString *dicPath = [documentPath stringByAppendingPathComponent:@"dicPath.plist"];

BOOL dicResult = [dic writeToFile:dicPath atomically:YES];

 if (dicResult == YES) {

    NSLog(@"字典写入成功");

} else {

    NSLog(@"字典写入失败");

}

NSDictionary *readDic = [NSDictionary dictionaryWithContentsOfFile:dicPath];

NSLog(@"readDic = %@", readDic);




0 0
原创粉丝点击