NSFileHandle
2015-11-20 15:48 xiangjune 阅读(174) 评论(0) 编辑 收藏 举报
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [pathArray firstObject];
NSString *filePath = [documentPath stringByAppendingPathComponent:@"test.txt"];
// 读取文件 1.创建 fileHandle 2.读写操作 3.关闭文件
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
NSData *data = [fileHandle readDataToEndOfFile];
NSString *content = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
[fileHandle closeFile];
// 写入文件
NSFileHandle *writeFileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
content = [content stringByAppendingString:@" new writing..."];
NSData *writeData = [content dataUsingEncoding:NSUTF8StringEncoding];
[writeFileHandle writeData:writeData];
[writeFileHandle closeFile];