IOS之数据持久化 (简单对象写入文件)

写入和读取

//写入

- (IBAction)wirteBT:(id)sender {
    //获取字符串
    NSString *str1 = _ONRTF.text;
    NSString *str2 = _TWOTF.text;

    //获取documents文件路径
    NSString *documentsPath  = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
#pragma mark -- NSString(字符串)写入本地
//    //拼接文件路径
//    //PathComponent 文件前不用加"/"
//    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"test.TXT"];
//    //写入
//    [str1 writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
#pragma mark -- 数组写入本地
//    //数组
//    NSArray *arr = @[str1,str2];
//    //拼接
//    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"arr.plist"];
//    //写入
//    [arr writeToFile:filePath atomically:YES];
#pragma mark -- 字典写入本地
//    //字典
//    NSDictionary *dic = @{@"one":_ONRTF.text,@"two":_TWOTF.text};
//    //文件拼接
//    NSString *firlPath = [documentsPath stringByAppendingPathComponent:@"dic.plist"];
//    //写入
//    [dic writeToFile:firlPath atomically:YES];
#pragma mark -- data写入本地
    //data
    NSData *data = [str1 dataUsingEncoding:NSUTF8StringEncoding];
    //文件拼接
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"data.data"];
    //写入
    [data writeToFile:filePath atomically:YES];
}

//读取

- (IBAction)readBT:(id)sender {
    //获取文件路径
    //获取documents文件路径
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
#pragma mark -- 字符串读取
//    //拼接文件路径
//    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"test.TXT"];
//    //展示
//    self.TWOTF.text = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
#pragma mark -- 数组读取
//    //文件路径拼接
//    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"arr.plist"];
//    //读取
//    NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];
//   //展示
//    self.ONRTF.text = arr[0];
//    self.TWOTF.text = arr[1];
#pragma mark -- 字典读取
//    //路径
//    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"dic.plist"];
//    //读取
//    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
//    //展示
//    self.ONRTF.text = [dic valueForKey:@"one"];
//    self.TWOTF.text = [dic valueForKey:@"two"];
#pragma mark -- data读取
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"data.data"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    //展示
    NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    self.TWOTF.text = str;

    //self.ONRTF.text = [data ];
 //   NSLog(@"%@",data);
}

创建,移动,复制,删除(清除缓存)

//创建
- (IBAction)creatBT:(id)sender {
    //获取documents 文件路径
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
    //拼接文件夹路径
    NSString *filePath = [documentPath stringByAppendingPathComponent:@"Images"];
    //创建文件管理对象
    NSFileManager *manager =  [NSFileManager defaultManager];
    //创建
    [manager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];

}

//移动

- (IBAction)moveBT:(id)sender {
    //将Images 移动到Caches中

    //oldPath
    //获取tmp文件路径
    NSString *tempPath = NSTemporaryDirectory();
    //拼接
    NSString *oldPath = [tempPath stringByAppendingPathComponent:@"Images"];
    //newPath
    //获取Caches文件路径
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];
    //拼接
    NSString *newPath = [cachesPath stringByAppendingPathComponent:@"Images"];
    //创建文件管理对象
    NSFileManager *manager = [NSFileManager defaultManager];

    //移动
    [manager moveItemAtPath:oldPath toPath:newPath error:nil];

}

//复制

- (IBAction)copyBT:(id)sender {
    //复制到tmp

    //1; 获取tmp文件路径
    NSString *tmp = NSTemporaryDirectory();
    //2: 文件拼接
    NSString *newPath = [tmp stringByAppendingPathComponent:@"Images"];
    //获取documents文件路径
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
    //文件拼接
    NSString *oldPath = [documentsPath stringByAppendingPathComponent:@"Images"];

    //创建文件管理对象
    NSFileManager *manager = [NSFileManager defaultManager];
    //复制
    [manager copyItemAtPath:oldPath toPath:newPath error:nil];

}

//删除文件

- (IBAction)removeBT:(id)sender {
    //获取Caches文件路径
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];
    //拼接
    NSString *filePath = [cachesPath stringByAppendingPathComponent:@"Images"];
    //创建文件管理对象
    NSFileManager *manager = [NSFileManager defaultManager];

    [manager removeItemAtPath:filePath error:nil];
}

是否存在

- (IBAction)exeitAction:(id)sender {
    //获取Caches文件路径
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];
    //拼接
    NSString *filePath = [cachesPath stringByAppendingPathComponent:@"Images"];
    //创建文件管理对象
    NSFileManager *mananger = [NSFileManager defaultManager];
    //判断是否存在
    BOOL isExist = [mananger fileExistsAtPath:filePath];
    UIAlertView *alerview = [[UIAlertView alloc]initWithTitle:@"提示" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    if (isExist) {
        alerview.message = @"文件存在";

    }else
    {
    alerview.message = @"文件不存在";
    }
    [alerview show];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值