NSFileHandle类主要对文件内容进行读取和写入操作

NSFileManager类主要对文件的操作(删除、修改、移动、复制等等)

常用处理方法

+ (id)fileHandleForReadingAtPath:(NSString *)path  打开一个文件准备读取

+ (id)fileHandleForWritingAtPath:(NSString *)path  打开一个文件准备写入

+ (id)fileHandleForUpdatingAtPath:(NSString *)path  打开一个文件准备更新

-  (NSData *)availableData; 从设备或通道返回可用的数据

-  (NSData *)readDataToEndOfFile; 从当前的节点读取到文件的末尾

-  (NSData *)readDataOfLength:(NSUInteger)length; 从当前节点开始读取指定的长度数据

-  (void)writeData:(NSData *)data; 写入数据

-  (unsigned long long)offsetInFile;  获取当前文件的偏移量

-  (void)seekToFileOffset:(unsigned long long)offset; 跳到指定文件的偏移量

-  (unsigned long long)seekToEndOfFile; 跳到文件末尾

-  (void)truncateFileAtOffset:(unsigned long long)offset; 将文件的长度设为offset字节

-  (void)closeFile;  关闭文件

/*    向文件添加数据*/

向文件追加数据

//获取沙盒Documents路径

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentDictionary = [paths objectAtIndex:0];

//创建文件

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *testDictionary = [documentDictionary stringByAppendingPathComponent:@"test"];

[fileManager createDirectoryAtPath:testDictionary withIntermediateDirectories:YES attributes:nil error:nil];

NSString *sourcePath = [testDictionary stringByAppendingPathComponent:@"testfile.text"];

//先写一个空字符进去

NSString *tempStr = @“”;

[tempStr writeToFile:sourcePath atomically:YES encoding:NSUTF8StringEncoding error:nil];

//打开这个testfile文件准备更新

NSFileHandle *fielHandle = [NSFileHandle fileHandleForUpdatingAtPath:sourcePath];

[fileHandle seekToEndOfFile];  //将节点跳到文件的末尾

NSString *str = @"追加的数据"  ;

NSData* stringData  = [str dataUsingEncoding:NSUTF8StringEncoding];

[fileHandle writeData:stringData]; 追加写入数据

//读取数据

NSData *reader = [NSData dataWithContentsOfFile:_testHeartFilePath];

NSString *tempStr = [[NSString alloc] initWithData:reader2 encoding:NSUTF8StringEncoding];

[fileHandle closeFile];

最新文章

  1. 准备CLR源码阅读环境
  2. vs2015启动网站调试提示 HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容。 解决方法
  3. WPF学习笔记——依赖属性(Dependency Property)
  4. django复习笔记1:环境配置
  5. AngularJS API之isXXX()
  6. pip 安装psycopg的错误
  7. switch为什么不能用string类型?
  8. 使用Javascript获得网页中通过GET方法提交的参数
  9. 百度Clouda的初步探索
  10. Dojo实现Tabs页报错(一)
  11. 【原创】poj ----- 1611 The Suspects 解题报告
  12. Python 异常处理
  13. C#版 - Leetcode 191. Number of 1 Bits-题解
  14. AI佳作解读系列(二)——目标检测AI算法集杂谈:R-CNN,faster R-CNN,yolo,SSD,yoloV2,yoloV3
  15. AGC004F Namori 树形DP、解方程(?)
  16. DbProviderFactory
  17. 通过Nginx反向代理之后客户端验证码session不一致造成无法验证通过的问题解决
  18. 随笔 js-----------------------------------------------------------------------------------------------------
  19. 解决Tax discount configure 报出异常
  20. <history> 特别报道:Google离职富翁们都在干什么?

热门文章

  1. CentOS7-部署测试Apollo
  2. [转帖]k8s 基本使用(下)
  3. 洛谷P5017:摆渡车——题解
  4. Docker 制作定制asp.netcore 的容器
  5. 关于mysql的null相关查询的一些坑
  6. scxml 图像展示器 (基于C++ MFC GDI tinyxpath的实现)
  7. 1-python运算符和逻辑控制语句
  8. linux ssh scp免密码
  9. Shell 选择排序
  10. SSM相关知识梳理面试