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)介绍和常用使用方法的更多相关文章

  1. OC中文件的操作

    OC中文件操作,在之前的文章中,已经接触到了文件的创建了,但是那不是很具体和详细,这篇文章我们就来仔细看一下OC中是如何操作文件的: 第一.首先来看一下本身NSString类给我们提供了哪些可以操作文 ...

  2. (Unity)XML文件读写与IO文件操作类使用介绍

    using System.Xml;                //xml文件操作命名空间 #region 写入操作 void WriteXMLFile(string _fileName) { Xm ...

  3. CSV文件读取类

    最近项目中,经常需要读取Csv文件.基本步骤是: (1)按行读取 (2)然后将一行数据按逗号,分割为字符串数组 (3)将各列字符串转换成相应类型的数据 ,如int double类型 写了一个简单的Cs ...

  4. [BS-18] 对OC中不可变类的理解

    对OC中不可变类的理解 OC中存在很多不可变的类(如NSString,NSAttributedString,NSArray,NSDictionary,NSSet等),用它们创建的对象存在于堆内存中,但 ...

  5. Perl中文件读取操作

    Perl中文件读取操作 http://blog.csdn.net/yangxuan12580/article/details/51506216

  6. MFC中文件对话框类CFileDialog详解及文件过滤器说明

    当前位置 : 首页 » 文章分类 :  开发  »  MFC中文件对话框类CFileDialog详解及文件过滤器说明 上一篇 利用OpenCV从摄像头获得图像的坐标原点是在左下角 下一篇 Word中为 ...

  7. 在OC中调用Swift类中定义delegate出现:Property 'delegate' not found on object of type ...

    找了许久没找到答案, 在下面的链接中, 我解决了这个问题: http://stackoverflow.com/questions/26366082/cannot-access-property-of- ...

  8. Oracle数据库中调用Java类开发存储过程、函数的方法

    Oracle数据库中调用Java类开发存储过程.函数的方法 时间:2014年12月24日  浏览:5538次 oracle数据库的开发非常灵活,不仅支持最基本的SQL,而且还提供了独有的PL/SQL, ...

  9. Java项目中每一个类都可以有一个main方法

    Java项目中每一个类都可以有一个main方法,但只有一个main方法会被执行,其他main方法可以对类进行单元测试. public class StaticTest { public static ...

随机推荐

  1. js实现replaceAll功能

    js中没有原生的replaceAll 方法. function replaceAll(str , replaceKey , replaceVal){ var reg = new RegExp(repl ...

  2. Javascript中的apply、call、bind

    apply . call .bind 三者都是用来改变函数的this对象的指向的: apply . call .bind 三者第一个参数都是this要指向的对象,也就是想指定的上下文: apply . ...

  3. Django框架之路由

    1,路由系统就是url系统,整个url系统就是请求进入Django项目的入口,每一个请求的种类由url分析完毕并再去返回相应的响应,它的本质是url与要为该url调用的视图函数之间的映射关系表(当项目 ...

  4. Koa2学习(八)使用session

    Koa2学习(八)使用session koa2框架不提供session的处理方法,这里我们需要借助一个第三方中间件koa-session来处理session. 先安装插件: $ npm i koa-s ...

  5. POJ1177 Picture —— 求矩形并的周长 线段树 + 扫描线 + 离散化

    题目链接:https://vjudge.net/problem/POJ-1177 A number of rectangular posters, photographs and other pict ...

  6. os.path.dirname(__file__)和os.path.abspath(__file__)区别

  7. MySQL中怎么查询一张表的列数

    select count(1) from information_schema.columns where table_schema='dbname' and table_name='tbname;

  8. 视图模板中 使用boottstrap 将各表单字段排成一行

    如果需要创建一个表单,它的所有元素是内联的,向左对齐的,标签是并排的,请向 <form> 标签添加 class .form-inline. <form class="for ...

  9. 【性能测试】服务器资源监测工具sar安装

    [root@yyy ~]# sar Cannot open /var/log/sa/sa19: No such file or directory 在Linux系统中,运行sar命令,发现无法执行: ...

  10. Python函数缓存

    函数缓存 (Function caching) 函数缓存允许我们将一个函数对于给定参数的返回值缓存起来.当一个I/O密集的函数被频繁使用相同的参数调用的时候,函数缓存可以节约时间.在Python 3. ...