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

     

      NSHomeDirectory(); //获取当前路径

      stringByAppendingPathComponent:@"/file1" 新建文件的路径

     

      NSFileManager: 文件管理类

      [NSFileManager defaultManager]; //初始化 在文件操作中:用defaultmanager 代替alloc]init];

     fileManager fileExistsAtPath:filePath  判断路径中是否已经存在该名字文件

     fileManager createFileAtPath:filePath contents:nil attributes:nil 创建文件

     

     

     native类写入文件:

      1、写入文件方法一:

     dataUsingEncoding:NSUTF8StringEncoding];// 对象方法  把内容转化成data

     NSFileHandle fileHandleForWritingAtPath:filePath];//类方法  准备写入-打开文件

     [fileHandle writeData:strData];//对象方法写数据

     [fileHandle closeFile];//对象方法关闭文件

     

      2、写入文件方法二:(这是覆盖性的写入,原数据被清除)

     writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];

     

     非native类写入文件:

     NSData *data = [NSKeyedArchiver archivedDataWithRootObject:per]; //对象转化为数据

     writeToFile:@"file3" atomically:YES  //对象方法 将数据写入文件

              读取文件:

     NSData *perData = [NSData dataWithContentsOfFile:@"file3"]; //读取文件数据

     Person *per1 = [NSKeyedUnarchiver unarchiveObjectWithData:perData]; //数据转化为对象并存储到per1

  • 相关阅读:
    postgresql-磁盘空间不足问题排查
    postgresql-删除重复数据
    postgresql-排序
    磁盘耗时时间
    nginx 报错 The plain HTTP request was sent to HTTPS port
    nodejs使用pkg打包
    npm 设置镜像源
    IDEA通过Ctrl+鼠标滚轮放大/缩小字体
    使 nodejs 代码 在后端运行(nohup)
    加密HSQLDB的.script文件
  • 原文地址:https://www.cnblogs.com/song-kl/p/4469110.html
Copyright © 2011-2022 走看看