IOS读取文件类和常用方法
来源:互联网 发布:淘宝详情页分析报告 编辑:程序博客网 时间:2024/06/06 00:59
第一、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];
0 0
- IOS读取文件类和常用方法
- IOS读取文件类和常用方法
- IOS读取文件类和常用方法
- 读取文件类和常用方法
- 属性文件读取常用方法
- java文件读取常用方法
- ios 读取文件有两种方法
- iOS文件读取方法 节选
- OC视频笔记-读取文件类常用方法NSFileHandle
- iOS开发时读取plist文件创建和读取的方法
- IOS常用的一些管理文件和目录方法
- perl正则表达式,文件读取常用方法
- iOS四种方法读取文件内容
- iOS 读取json文件和读取plist文件
- ios之读取文件类
- iOS 文件的读取和写入
- iOS 文件的读取和写入
- iOS plist文件写入和读取
- 查看linux可用内存
- Linux中删除空行及
- java中用httpClient请求service时bug解决方法(httpGet)
- js中window.open弹出的是上次的缓存页面解决办法
- java 生成xml格式文件
- IOS读取文件类和常用方法
- 测序中常用的术语
- 力导向图link直接加提示
- 使用亚马逊 CloudFront 创建 RTMP 的 CDN 加速
- mysqlnd 插件mysqlnd_ms和mysqlnd_qc的介绍
- 黑马程序员 知识点总结-Java内部类
- Android自动化测试之Monkey
- 手动添加mysql服务
- ffmpeg 重要结构体说明