IOS读取文件类和常用方法
来源:互联网 发布:天猫跟淘宝是什么关系 编辑:程序博客网 时间:2024/05/22 06:46
第一、NSFileHandle
NSFileManager类主要对文件的操作(删除、修改、移动、复制等)
NSFileHandle类主要对文件的内容进行读取和写入
第二、NSFileHandle类处理文件的步骤
创建一个NSFileHandle对象
对打开的文件进行I/0操作
关闭文件
可以使用NSFileHandle进行断点续传
第三、实现查找功能的代码:
NSString *homePath=NSHomeDirectory();
NSString *filePath=[homePath stringByAppendingPathComponent:@"Desktop/hello.rtf"];
NSFileHandle *fileHandle=[NSFileHandle fileHandleForReadingAtPath:filePath];
NSUInteger length=[fileHandle availableData].length;
[fileHandle seekToFileOffset:length/2];
NSData *data=[fileHandle readDataToEndOfFile];
NSString *str=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
第四、实现追加文件代码:
NSString *homePath=NSHomeDirectory();
NSString *filePath=[homePath stringByAppendingPathComponent:@"Desktop/hello.rtf"];
NSFileHandle *fileHandle=[NSFileHandle fileHandleForUpdatingAtPath:filePath];
//[fileHandle seekToEndOfFile];
[fileHandle seekToFileOffset:10];
NSString *str=@"data";
NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding];
[fileHandle writeData:data];
[fileHandle closeFile];
// insert code here...
NSLog(@"Hello, World!");
第五、实现复制文件的代码:
NSString *homePath=NSHomeDirectory();
NSString *filePath=[homePath stringByAppendingPathComponent:@"Desktop/hello.rtf"];
NSString *objPath=[homePath stringByAppendingPathComponent:@"Desktop/copy.rtf"];
NSFileManager *fileManager=[NSFileManager defaultManager];
BOOL success=[fileManager createFileAtPath:objPath contents:nil attributes:nil];
NSFileHandle *writeFile=[NSFileHandle fileHandleForReadingAtPath:filePath];
NSFileHandle *objFile=[NSFileHandle fileHandleForWritingAtPath:objPath];
[objFile readDataToEndOfFile];
NSData *data=[writeFile readDataToEndOfFile];
[objFile writeData:data];
[writeFile closeFile];
[objFile closeFile];
- IOS读取文件类和常用方法
- IOS读取文件类和常用方法
- IOS读取文件类和常用方法
- 读取文件类和常用方法
- 属性文件读取常用方法
- java文件读取常用方法
- ios 读取文件有两种方法
- iOS文件读取方法 节选
- OC视频笔记-读取文件类常用方法NSFileHandle
- iOS开发时读取plist文件创建和读取的方法
- IOS常用的一些管理文件和目录方法
- perl正则表达式,文件读取常用方法
- iOS四种方法读取文件内容
- iOS 读取json文件和读取plist文件
- ios之读取文件类
- iOS 文件的读取和写入
- iOS 文件的读取和写入
- iOS plist文件写入和读取
- Linux静态库和动态库(转)
- 在Windows下搭建Git服务器
- Android--将数据保存到SDCard中
- 二分图的最大匹配-hdu-3729-I'm Telling the Truth
- 当onclick无效的时候 可以尝试使用return false来解决
- IOS读取文件类和常用方法
- windows判断用户是否在使用键盘鼠标的API
- hdu 3853 LOOPS
- Android不同apk之间传递参数与数据
- H264参数结构二:网络提取层NAL (Net Abstraction Layer) & 视频编码层VCL (Video Coding Layer) .
- plupload 中文API
- IE兼容问题 在chrome中居中 在IE系列下就靠左
- TCP连接概念及c++编程 (整理)
- 浮点数比较问题