OC中文件读取类(NSFileHandle)介绍和常用使用方法
2024-08-23 15:14:17
NSFileHandle
1.NSFileManager类主要对于文件的操作(删除,修改,移动,赋值等等)
//判断是否有 tagetPath 文件路径,没有就创建
NSFileManager *fileManage = [NSFileManager defaultManager];
BOOL success = [fileManage createFileAtPath:tagetPath contents:nil attributes:nil];
if (success) {
NSLog(@"create success");
}
2.NSFileHandle类主要对文件的内容进行读取和写入操作
①NSFileHandle处理文件的步骤
1:创建一个NSFileHandle对象
//创建流
NSFileHandle *inFileHandle = [NSFileHandle fileHandleForReadingAtPath:srcPath];
NSFileHandle *outFileHandle = [NSFileHandle fileHandleForWritingAtPath:tagetPath];
2:对打开的文件进行I/O操作
//获取文件路径
NSString *homePath = NSHomeDirectory();
NSString *filePath = [homePath stringByAppendingPathComponent:@"phone/Date.text"];
NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
//定位到最后
[fileHandle seekToEndOfFile];
//定位到某个位置,100字节之后
[fileHandle seekToFileOffset:100];
//追加的数据
NSString *str = @"add world";
NSData *stringData = [str dataUsingEncoding:NSUTF8StringEncoding];
//追加写入数据
[fileHandle writeData:stringData];
3:关闭文件对象操作
//关闭流
[fileHandle closeFile];
常用处理方法,读
//读取文件内容
void readByFile(){
//文件路径
NSString *homePath = NSHomeDirectory();
NSString *filePath = [homePath stringByAppendingPathComponent:@"phone/cellPhone.text"];
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
NSInteger length = [fileHandle availableData].length;
//跳转到指定位置
[fileHandle seekToFileOffset:length/2];
NSData *data = [fileHandle readDataToEndOfFile];
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
[fileHandle closeFile];
}
复制文件
void copy2Other(){
NSString *homePath = NSHomeDirectory();
NSString *filePath = [homePath stringByAppendingPathComponent:@"phone/cellPhone.text"];
NSString *tagetPath = [homePath stringByAppendingPathComponent:@"phone/cellPhone_bak.text"];
//是否有这个文件,没有则创建
NSFileManager *fileManage =[NSFileManager defaultManager];
BOOL success = [fileManage createFileAtPath:tagetPath contents:nil attributes:nil];
if (success) {
NSLog(@"create success");
}
//通过 NSFileHandle 读取源文件,写入另一文件中
NSFileHandle *outFileHandle = [NSFileHandle fileHandleForWritingAtPath:tagetPath];
NSFileHandle *inFileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
NSData *data = [inFileHandle readDataToEndOfFile];
[outFileHandle writeData:data];
//关闭流
[inFileHandle closeFile];
[outFileHandle closeFile];
}
最新文章
- VMWare虚拟机下为Ubuntu 12.04.1配置静态IP(NAT连接方式)
- linux的三种安装软件包的方式(小白的学习之旅)
- Ninject之旅之十一:Ninject动态工厂(附程序下载)
- 8 个必备的PHP功能开发
- Office 365 - SharePoint Tips &; Tricks
- ZOJ 1125 Floating Point Numbers
- css新增UI样式
- Flex疑难小杂症
- 在linux中安装git,并将代码发布到github
- 大数据平台搭建-spark集群安装
- SecureCRT 历史版本下载
- redis客户端可以连接集群,但JedisCluster连接redis集群一直报Could not get a resource from the pool
- 面向对象_item项目
- Laravel 服务容器,IoC,DI
- 入坑C++
- IT老人,给后辈的十一点建议
- 跟踪OceanLotus的新下载程序KerrDown
- ElasticSearch 2 (37) - 信息聚合系列之内存与延时
- 不借助autolt实现下载文件到指定目录
- django之创建第4-2个项目-访问class类属性和类方法
热门文章
- Override is not allowed when implementing interface method Bytecode Version Overriding and Hiding Methods
- WCF Rest post请求
- sqlserver游标使用误区
- Java 内部类理解
- 小程序-demo:小熊の日记
- Java-Maven:Maven百科
- VS2010打包回顾
- Ubuntu 安装MTP驱动访问安卓设备(转载)
- bzoj 1912: [Apio2010]patrol 巡逻【不是dp是枚举+堆】
- 【插件开发】—— 14 Site is incorrect!编辑器启动报错!