IOS 文件读写 数据持久化

/**
 * 数据持久化的本质 将数据读成文件存储在本地
 沙盒机制 就是系统针对每个应用程序在本地生成的文件夹 名字随机生成 对于不同的应用程序 不能访问其他应用程序沙盒的内容 起到保护作用
 1)Documents:  用来存储长久保存的数据
 2)xxx.app: 应用程序的包 包含应用程序加载所需的所有资源 (readOnly 只读 不可修改) 平时使用的 NSBundle就是该包
 3)library : A:(caches) : 本地缓存 存储想暂时保存的数据 比如 下载的 视频音频图片 都存在该文件下 (自己新建文件 : videos musics images)
                : B:(perferences) 存储用户的偏好设置 比如程序是否是第一次启动
 4)tem : 存储 还未下载完的 视频 音频 当下载完毕 将文件移动到caches 文件夹下
 */
-(void)handleWrite:(UIButton *)btn{

    //写入时 将第一个输入框的文字 写入点击事件
//    1)获取存储的内容
    UITextField *tf = (UITextField *)[self.view viewWithTag:100];
    NSString *content = tf.text;
    UITextField *tf1 = (UITextField *)[self.view viewWithTag:101];
    NSString *content1 = tf1.text;
        // 2) 获取到所要存储的文件路径
//    1.获取文件夹路径
    /**
     *  用来获取指定文件夹路径
     *
     *  @param NSDocumentDirectory  指定文件夹 是document 还是library
     *  @param NSUserDomainMask  设置查找的域 我们自己的文件都是存储在用户域的
     *  @param YES                 是否使用 详细路径
     *
     *  @return  最初该方法适用于MacOs的而对于电脑系统来说 可能会存储多个用户 所以获取的路径会有多条 返回值是数组 但在 ios下 只有一个用户 数组中只有一个原素
     */
//    NSString *newFilePath = [self getFilePath];
//    3) 将内容存储到指定文件路径
//    1.字符串写入本地文件
//    NSError *error = nil;
//    BOOL isSucceed =  [content writeToFile:newFilePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
//    2. 数组写入本地文件
//    NSArray *arr = @[content,content1];
//    BOOL isScceed = [arr writeToFile:[self getFilePath] atomically:YES];
//    NSLog(@"%d",isScceed);
    
    NSDictionary *dic =@{@"tf1" : content,@"tf2" : content1};
    BOOL isSucceed =[dic writeToFile:[self getFilePath] atomically:YES];
    NSLog(@"%d",isSucceed);
    
};
-(void)handleRead:(UIButton *)btn{
    //每次写入都会将内容覆盖掉 如果需要保留之前的数据 需先将之前的数据拼接在一起 一块儿写入
//    1.字符串从本地文件读取
//    NSError *error = nil;
//    NSString *content = [NSString stringWithContentsOfFile:[self getFilePath] encoding:NSUTF8StringEncoding error:&error];
    UITextField *tf = (UITextField *)[self.view viewWithTag:100];
    UITextField *tf1 = (UITextField *)[self.view viewWithTag:101];
//    tf.text = content;
//    2.数组从本地文件读取
//    NSArray *arr = [NSArray arrayWithContentsOfFile:[self getFilePath]]; 
//   tf1.text = arr[1];
//    tf.text = arr[0];
//    3.从字典读取文件
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:[self getFilePath]];
    tf.text = dic[@"tf2"];
    tf1.text = dic[@"tf1"];
}
/**
 *  文件读写暂时只支持 数组 字符串 字典 二进制流(nsdata)及他们的子类
 *写入文件 : writeToFile:(这是对象调用的方法)
    读取文件 : 每个类 自带的能够根据文件路径创建对象的方法 [类名 类WithContentOfFile:]
 (牢牢谨记) 对于数组字典这种容器来说内部的成员也必须是能够实现读写的八个类之一
  */
-(NSString *)getFilePath{
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *newFilePath = [documentsPath stringByAppendingPathComponent:@"aa.txt"];
    return newFilePath;
}

// 对于 非以上八大类之一的类 如(自定义的类)则需要使用归档 与反归档

-(void)handleArchive:(UIButton *)btn{
//    1)获取输入框的内容
    UITextField *tf = (UITextField *)[self.view viewWithTag:100];
    UITextField *tf1 = (UITextField *)[self.view viewWithTag:101];
    NSString *content = tf.text;
    NSString *content1 = tf1.text;
//    2)封装成Person对象
    Person *per = [[Person alloc]initWithName:content gender:content1 age:18];
//    3)创建归档对象
    NSMutableData *data = [NSMutableData data];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
//    4) 归档
    [archiver encodeObject:per forKey:@"110"];
//    5) 结束归档,当结束归档后再归档无效
    [archiver finishEncoding];
    [per release];
    [archiver release];
//    data 写入文件
    [data writeToFile:[self getFilePath] atomically:YES];
    NSLog(@"归档");
}
-(NSString *)getFilePath{
//    1.获得文件夹路径
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *newFilePath = [documentsPath stringByAppendingPathComponent:@"bb.txt"];
    NSLog(@"%@",newFilePath);
    return newFilePath;
}
-(void)handleDisarchive:(UIButton *)btn{
//    1)根据文件路径初始化
    NSMutableData *mdate = [NSMutableData dataWithContentsOfFile:[self getFilePath]];
    NSLog(@"%@",mdate);
//    2)创建反归档对象
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:mdate];
//    3)反归档
    Person *per = [unarchiver decodeObjectForKey:@"110"];
    [unarchiver finishDecoding];
    [unarchiver release];
    UITextField *tf = (UITextField *)[self.view viewWithTag:100];
    UITextField *tf1 = (UITextField *)[self.view viewWithTag:101];
    tf.text = per.gender;
    tf1.text = per.name;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值