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;

    }

  • 相关阅读:
    安装kali中的一些常用工具和firefox的插件
    kali定制
    熟悉BASH命令
    kali中各目录的作用
    kali中netspeed的安装方法
    kali中常用的ctf工具
    【学生党必看】大学里,你的六个重要选择【转载】
    kali持久加密USB的制作
    elasticsearch _search结果解析
    Springboot整合Elasticsearch报错availableProcessors is already set to [4], rejecting [4]
  • 原文地址:https://www.cnblogs.com/wukun168/p/6010821.html
Copyright © 2011-2022 走看看