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