zoukankan      html  css  js  c++  java
  • 文件读写

    /**

     *   数据持久化方式:

       1.属性列表 plist文件

       2.文件读写: 简单对象

       3.归档反归档 : 复杂对象.

       4.sqlite

      5.CoreData;

     */

    /**

       简单对象可直接通过文件读写来操作

      *4种类型支持文件读写: 字符串,数组,字典,二进制数据.

     ##数组和字典如果直接文件读写操作,其中的元素也必须是以上支持的4种类型.

     */

    //字符串的写入路径

    - (NSString *)stringFilePath{

        //获取Documents文件夹路径

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

        //stringByAppendingPathComponent 在指定的路径下拼接一个对应名称的子文件.

        //拼接一个文件路径

      NSString *stringPath =  [docPath stringByAppendingPathComponent:@"string.txt"];

        return stringPath;

    }

    //写入字符串

    - (IBAction)StringWrite:(id)sender {

        //将第一个输入框的文本写入文件

        //获取写入的文件路径

        //写入文件.

        NSString *path = [self stringFilePath];

       BOOL isWrote =  [self.tf1.text  writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];

        NSLog(@"%@",isWrote ?@"成功":@"失败");

    }

    //读取字符串

    - (IBAction)stringRead:(id)sender {

        //获取文件路径

        NSString *path = [self stringFilePath];

        NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

        NSLog(@"%@",str);

    //    self.tf2.text = str;

    }

    //指定数组写入的文件路径

    - (NSString *)arrayFilePath{

        return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject]stringByAppendingPathComponent:@"arr.doc"];

    }

    //写入数组

    - (IBAction)arrayWrite:(id)sender {

         //获取写入的文件路径

        NSString *file = [self arrayFilePath];

        //将tf1和tf2的文本存放到数组,写入.

        NSArray *arr = @[self.tf1.text,self.tf2.text];

        [arr writeToFile:file atomically:YES];

    }

    //读取数组

    - (IBAction)arrayRead:(id)sender {

         NSString *file = [self arrayFilePath];

        NSArray *arr = [NSArray arrayWithContentsOfFile:file];

        NSLog(@"%@",arr);

    }

    //写入字典路径

    - (NSString *)dicPath{

        return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject]stringByAppendingPathComponent:@"dic.plist"];

    }

    - (IBAction)dicWrite:(id)sender {

        NSDictionary *dic = @{@"tf1":self.tf1.text,@"tf2":self.tf2.text};

        NSString *file = [self dicPath];

        [dic writeToFile:file atomically:YES];

    }

    - (IBAction)dicRead:(id)sender {

        NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:[self dicPath]];

        NSLog(@"%@",dic);

    }

    //指定data写入文件路径

    - (NSString *)dataFile{

        return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject]stringByAppendingPathComponent:@"data.data"];

    }

    //写入二进制数据

    - (IBAction)dataWrite:(id)sender {

        NSString *path = [self dataFile];

        //将tf1的文本转为NSData写入

        NSData *data = [self.tf1.text dataUsingEncoding:NSUTF8StringEncoding];

        [data writeToFile:path atomically:YES];

        //将image转为NSData

       /* UIImagePNGRepresentation(<#UIImage * _Nonnull image#>)*/

        //从NSData数据初始化得到对应的image

        /*

        UIImage *image = [UIImage imageWithData:<#(nonnull NSData *)#>]*/

        

    }

    //读取二进制数据

    - (IBAction)dataRead:(id)sender {

        NSData *data = [NSData dataWithContentsOfFile:[self dataFile]];

        //将存储tf1文本数据显示tf2上

        NSString *str= [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

        self.tf2.text = str;

    }

  • 相关阅读:
    目录创建用什么命令?创建文件用什么命令?复制文件用什 么命令?
    哪些浏览器支持HTML 5?
    终端是哪个文件夹下的哪个文件?黑洞文件是哪个文件夹下 的哪个命令?
    什么是端到端微服务测试?
    HTML 5中的本地存储概念?
    HTML 5的页面结构和HTML 4或早先的HTML有什么不同?
    Spring 切面可以应用五种类型的通知?
    Math.round(11.5) 等于多少?Math.round(-11.5)等于多少?
    微服务架构如何运作?
    双因素身份验证的凭据类型有哪些?
  • 原文地址:https://www.cnblogs.com/wukun168/p/6010821.html
Copyright © 2011-2022 走看看