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

UI 沙盒机制 文件的读写操作

时间:2014-09-10 00:30:29      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:文件的读写 沙盒机制

沙盒机制:

数据持久化的本质:将数据读成文件,存储在本地

沙盒机制:沙盒就是系统针对于每个应用程序在本地生成的文件夹(名字随机生成).对于不同的应用程序,不能访问其他应用程序沙盒内的内容.对于该应用内容起到保护作用.

沙盒内的文件夹:

(1)Documents:用来存储长久保存的数据

(2)XXX.app: 应用程序的包,包含应用程序加载所需的所有资源,(readonly 只读,不可修改).平时使用的NSBundle就是该包.

(3)Library:

A: Caches:本地缓存,存储想暂时保存的数据.比如下载的视频,音频,图片都存储在该文件夹下(Videos, Musics, Images).

B: tmp:存储还未下载完的视频, 音频, 当下载完毕之后将文件转移到Caches文件夹下

文件的读写:

文件读写暂时只支持NSString, NSArray, NSDictionary, NSData 以及他们的子类

写入文件:writeToFile:(这是对象调用的方法)

读取文件:每个类自带的能够根据文件路径创建对象的方法:[类名 类WithContentsOfFile:];

字符串:[NSString stringWithContentsOfFile:];

字典:[NSDictionary dictionaryWithOfFile:];

二流进制:[NSData dataWithContentsOfFile:];

(谨记:)对于数组,字典这种容器类来说,内部成员也必须是能够实现文件读写的八个类之一

//获取文件路径

- (NSString *)getFilePath

{

   //(1)获取Documents文件夹路径

    /**

     * 用来获取指定文件夹的路径

     *

     *  @param NSDocumentDirectory指定的文件夹(Documents,还是Library).

     *  @param NSUserDomainMask   设置查找的域, 我们自己的文件都是存储在用户域的

     *  @param YES                是否使用详细路径(绝对路径)

     *

     *  @return因为最初该方法是使用于MAC OS,而对于电脑系统来说,可能会存储在多个用户,所以获取到的路径可能会有多条,所以返回值类型是数组,但是对于iOS,就只有一个用户,所以数组中就只有一个元素.

     */

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

   //(2)拼接上要存储的文件路径

    NSString *newFilePath = [documentsPath stringByAppendingPathComponent:@"Tsummer.txt"];

    NSLog(@"%@", newFilePath);

    return newFilePath;

}


//写入操作

- (void)writeClick:(UIButton *)writeBtn

{

//1.获取存储的内容

UITextField *tf = (UITextField *)[self.viewviewWithTag:100];

    NSString *content = tf.text;

    UITextField *tf1 = (UITextField *)[self.viewviewWithTag:101];

   NSString *content1 = tf1.text;

   //2.获取到所要存储的文件路径

    NSString *newFilePath = [selfgetFilePath];

   //3.将内容存储到指定文件路径

   //(1)字符串写入本地文件

//    NSError *error = nil;

//    BOOL isSuccess = [content writeToFile:newFilePath atomically:YES encoding:NSUTF8StringEncoding error:&error];

//    NSLog(@"%d", isSuccess);

   //(2)数组写入本地文件

//    NSArray *arr = @[content, content1];

//    BOOL isSuccess = [arr writeToFile:newFilePath atomically:YES];

//    NSLog(@"%d", isSuccess);

   //(3)字典写入本地文件

    NSDictionary *dic = @{@"tf1":content,@"tf2":content1};

    BOOL isSuccess = [dic writeToFile:newFilePath atomically:YES];

    NSLog(@"%d", isSuccess);

}

//读操作

- (void)readClick:(UIButton *)readBtn

{

   //每次写入都会将之前的内容覆盖掉.

   //如果想保留之前的数据,需要先将之前的数据读出,然后和将要存储的数据拼接在一起,一块写入.

    NSString *newFilePath = [selfgetFilePath];

   //字符串从本地文件读取

   //    NSString *content = [NSString stringWithContentsOfFile:newFilePath encoding:NSUTF8StringEncoding error:nil];

   //(2)数组从本地文件读取

//    NSArray *arr = [NSArray arrayWithContentsOfFile:newFilePath];

   //(3)字典从本地文件读取

    NSDictionary *dic = [NSDictionarydictionaryWithContentsOfFile:newFilePath];

   UITextField *tf = (UITextField *)[self.viewviewWithTag:100];

    tf.text = dic[@"tf1"];

    UITextField *tf1 = (UITextField *)[self.viewviewWithTag:101];

    tf1.text = dic[@"tf2"];

}




UI 沙盒机制 文件的读写操作

标签:文件的读写 沙盒机制

原文地址:http://blog.csdn.net/mhtios/article/details/39162803

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