OC中文件读取类(NSFileHandle)介绍和常用使用方法
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];
}
OC中文件读取类(NSFileHandle)介绍和常用使用方法的更多相关文章
- OC中文件的操作
OC中文件操作,在之前的文章中,已经接触到了文件的创建了,但是那不是很具体和详细,这篇文章我们就来仔细看一下OC中是如何操作文件的: 第一.首先来看一下本身NSString类给我们提供了哪些可以操作文 ...
- (Unity)XML文件读写与IO文件操作类使用介绍
using System.Xml; //xml文件操作命名空间 #region 写入操作 void WriteXMLFile(string _fileName) { Xm ...
- CSV文件读取类
最近项目中,经常需要读取Csv文件.基本步骤是: (1)按行读取 (2)然后将一行数据按逗号,分割为字符串数组 (3)将各列字符串转换成相应类型的数据 ,如int double类型 写了一个简单的Cs ...
- [BS-18] 对OC中不可变类的理解
对OC中不可变类的理解 OC中存在很多不可变的类(如NSString,NSAttributedString,NSArray,NSDictionary,NSSet等),用它们创建的对象存在于堆内存中,但 ...
- Perl中文件读取操作
Perl中文件读取操作 http://blog.csdn.net/yangxuan12580/article/details/51506216
- MFC中文件对话框类CFileDialog详解及文件过滤器说明
当前位置 : 首页 » 文章分类 : 开发 » MFC中文件对话框类CFileDialog详解及文件过滤器说明 上一篇 利用OpenCV从摄像头获得图像的坐标原点是在左下角 下一篇 Word中为 ...
- 在OC中调用Swift类中定义delegate出现:Property 'delegate' not found on object of type ...
找了许久没找到答案, 在下面的链接中, 我解决了这个问题: http://stackoverflow.com/questions/26366082/cannot-access-property-of- ...
- Oracle数据库中调用Java类开发存储过程、函数的方法
Oracle数据库中调用Java类开发存储过程.函数的方法 时间:2014年12月24日 浏览:5538次 oracle数据库的开发非常灵活,不仅支持最基本的SQL,而且还提供了独有的PL/SQL, ...
- Java项目中每一个类都可以有一个main方法
Java项目中每一个类都可以有一个main方法,但只有一个main方法会被执行,其他main方法可以对类进行单元测试. public class StaticTest { public static ...
随机推荐
- android.view.View
* This class represents the basic building block for user interface components. A View * occupies a ...
- Hadoop 解除 “Name node is in safe mode”
运行Hadoop程序时,有时候会报以下错误: org.apache.hadoop.dfs.SafeModeException: Cannot delete /user/hadoop/input. N ...
- Latex 3: 解决LaTeX编译卡顿问题
1.问题: 最近在编译latex时,老是在tulmr.fd处编译很久,但是以前不这样啊,那肯定就是我最近做了什么导致这样的了,是什么呢? 2.解决: 后来google下发现了解决办法,原来是我新安装了 ...
- UVA11383 Golden Tiger Claw —— KM算法
题目链接:https://vjudge.net/problem/UVA-11383 题解: 根据KM()算法,标杆满足:l(x) + l(y) >= w(x, y) . 当求完最大权匹配之后,所 ...
- [Codeforces 1011E] Border
[题目链接] https://codeforces.com/contest/1011/problem/E [算法] 裴蜀定理 : 设为n个整数,d是它们的最大公约数,那么存在整数 使得 显然 , ...
- c#网格控件,Excel控件
http://www.grid2000.com/images.html Cell Type FlexCell supports following cell types: TextBox, Com ...
- .NET获取汉字首字母
/// <summary> /// 获取汉字首字母(可包含多个汉字) /// </summary> /// <param name="strText" ...
- C++中class在实例化前到底占用多少空间
最近忽然想起一个问题,C++中class在实例化前到底占用多少空间?我隐约记得书上是这么说的“类在实例化时才分配空间”!但是当我写下这个小程序的时候,杯具产生了—— #include<iostr ...
- bzoj 4823: [Cqoi2017]老C的方块【最大权闭合子图】
参考:https://www.cnblogs.com/neighthorn/p/6705785.html 并不是黑白染色而是三色染色(还有四色的,不过是一个意思 仔细观察一下不合法情况,可以发现都是特 ...
- bzoj3550: [ONTAK2010]Vacation(单纯形法+线性规划)
传送门 直接暴力把线性规划矩阵给打出来然后单纯形求解就行了 简单来说就是每个数记一个\(d_i\)表示选或不选,那么就是最大化\(\sum d_ic_i\),并满足一堆限制条件 然后不要忘记限制每个数 ...