在现阶手机app的临时缓存文件渐渐增多,在app开发中对于移动设备文件的操作越来越多,我们IOS中对于文件的操作主要涉及两个类NSFileManager 和NSFileHandle,下面我们就看看如何使用这两个类:

1、文件创建

//初始化一个NSFileManager类defaultManager方法为单例模式,通过单例模式进行初始化
NSFileManager * fileManager =[NSFileManager defaultManager]; //拼接路径
NSString * path=NSHomeDirectory();
path=[path stringByAppendingPathComponent:@"deskTop/date.txt"]; //创建文件
BOOL flag=[fileManager createFileAtPath:path contents:nil attributes:nil];
if(flag){
NSLog(@"文件创建成功");
}else{
NSLog(@"文件创建失败");
}

2、创建目录

NSFileManager  * fileManager =[NSFileManager defaultManager];
NSString * path=NSHomeDirectory();
path=[path stringByAppendingPathComponent:@"deskTop/pro/cpp"];
BOOL flag=[fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil]; if(flag){
NSLog(@"创建成功");
}else{
NSLog(@"创建失败");
}

3、删除文件和目录

 NSFileManager  * fileManager =[NSFileManager defaultManager];
NSString * rootPath=NSHomeDirectory();
NSString * dirPath=[rootPath stringByAppendingPathComponent:@"deskTop/newFolder"]; NSArray * array=[fileManager contentsOfDirectoryAtPath:dirPath error:nil];
for(NSString * str in array){ NSString * newPath=[dirPath stringByAppendingPathComponent:str];
BOOL flag=[fileManager removeItemAtPath:newPath error:nil];
if(flag){
NSLog(@"删除成功");
}else{
NSLog(@"删除失败");
} }

对于文件的操作有很多方法我们为提及到,我们可以看下官方的API里面有很多我们可能会用到的方法

 //将一个文件复制到另一个文件
[fileManager copyItemAtPath:path1 toPath:path2 error:nil]; //将一个文件移动到另一个文件
[fileManager moveItemAtPath:path1 toPath:path2 error:nil];
//获取文件里面的内容
NSData * readData=[fileManager contentsAtPath:path]

  案例:我们这里可以做一个例子如何计算一个文件里面的所有行数,思路:我们首先需要读取文件里面的所有信息内容,统计行数我们只需要统计文件里的换行符的个数即可,实例如下所示:

NSFileManager  * fileManager =[NSFileManager defaultManager];
NSString * rootPath=NSHomeDirectory();
NSString * dirPath=[rootPath stringByAppendingPathComponent:@"deskTop/newFolder/main.m"]; NSData * data=[fileManager contentsAtPath:dirPath];
NSString * str=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; int count=(int)[str componentsSeparatedByString:@"\n"].count; NSLog(@"当前的总行数为%d",count);

4、文件写入

在文件的读写操作过程中我们用的最多的是NSFileHandler,我们通过该类进行文件操作

   //初始化一个用于写入的文件句柄
NSFileHandle * fileHandle=[NSFileHandle fileHandleForWritingAtPath:filePath]; NSString * str=@"pppppppppppp"; //将文件光标移动到文件的最后位置
[fileHandle seekToEndOfFile];
NSData * data=[str dataUsingEncoding:NSUTF8StringEncoding]; //写入数据
[fileHandle writeData:data]; //用完之后需要关掉
[fileHandle closeFile];

5、文件的读取

  NSFileHandle * fileHandle=[NSFileHandle fileHandleForReadingAtPath:filePath];
//读取到文件的末尾
NSData * data= [fileHandle readDataToEndOfFile];
//定位光标的位置
[fileHandle seekToFileOffset:];
//查询该文件可用数据的个数
[fileHandle availableData];
//读取指定长度的文件
NSData * data=[fileHandle readDataOfLength:];
NSString * str=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
//关闭句柄
[fileHandle closeFile];
作者:杰瑞教育
出处:http://www.cnblogs.com/jerehedu/ 
版权声明:本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

技术咨询:
 

IOS之NSFileManager 和NSFileHandle的更多相关文章

  1. ios NSFileManager和NSFileHandle(附:获取文件大小 )

    转自 http://blog.csdn.net/zhibudefeng/article/details/7795946 //file 文件操作 NSFileManager  常见的NSFileMana ...

  2. IOS文件系统及其相关操作(NSFileManager,NSFileHandle)

    How do you get the paths to these special sandbox directories? NSArray *NSSearchPathForDirectoriesIn ...

  3. iOS - OC NSFileManager 文件管理

    前言 @interface NSFileManager : NSObject @interface NSFileHandle : NSObject <NSSecureCoding> NSF ...

  4. OC语言--NSFileManager&amp; NSFileHandle

    1.关于文件的介绍 ->什么是文件: 文件概念, 广义文件. 狭义文件(磁盘文件). 文件常见的使用操作(可用命令行演示文件操作的使用场景). ->什么是路径: 简单来说就是,在系统中,要 ...

  5. 高级UIKit-03(NSFileManager、NSFileHandle)

    fileManager文件管理器 [day04_1_FileManager_Search] :查找文件 fileManager有一个方法可以判断文件是否是文件夹, fileExistsAtPath:i ...

  6. NSFileManager和NSFileHandle(附:获取文件大小 )

    本文转载至:http://www.cnblogs.com/pengyingh/articles/2350345.html 天牛 感谢原创作者的硕果 //file 文件操作 NSFileManager  ...

  7. iOS中NSFileManager文件常用操作整合

    //获取Document路径 + (NSString *)getDocumentPath { NSArray *filePaths = NSSearchPathForDirectoriesInDoma ...

  8. NSFileManager和NSFileHandle使用

    一.NSFileManager: 1.1.获取NSFileManager NSFileManager *manager = [NSFileManager defaultManager];     NS ...

  9. iOS NSFileManager对沙盒文件及目录添加删除操作

    iOS 使用 NSFileManager对沙盒里面的文件和目录,增加,修改,删除操作: - (void)viewDidLoad { [super viewDidLoad]; self.title = ...

随机推荐

  1. JAVAEE——宜立方商城02:服务中间件dubbo、工程改造为基于soa架构、商品列表实现

    1. 学习计划 第二天:商品列表功能实现 1.服务中间件dubbo 2.工程改造为基于soa架构 3.商品列表查询功能实现. 2. 将工程改造为SOA架构 2.1. 分析 由于宜立方商城是基于soa的 ...

  2. DMA

    DMA:如果将一串字符串通过串口传送到外设中去,用传统的方法,则CPU将不断的去扫描UTSTAT这个寄存器,在字符发送期间,CPU将不能做任何其他事情.为了解决这个问题,则在诞生了DMA CPU只需要 ...

  3. window下完全删除nodejs

    1.从卸载程序卸载程序和功能. 2.重新启动(或者您可能会从任务管理器中杀死所有与节点相关的进程). 3.寻找这些文件夹并删除它们(及其内容)(如果还有).根据您安装的版本,UAC设置和CPU架构,这 ...

  4. 【DFS好题】BZOJ1999- [Noip2007]Core树网的核(数据加强版)

    NOIP的数据好水,一开始有好几个错结果NOIP数据就水过了?? [题目大意] 求无根树的直径上一段不超过S长的链,使得偏心距最小.具体概念见原题. [思路] 首先明确几个性质: (1)对于树中的任意 ...

  5. Problem A: 象棋比赛

    Description 1月6日,教职工象棋协会在6号楼办了一次比赛,很多老师都参加了.比赛共进行了5轮,赢1局积3分,和了1分,输了0分,你能帮忙算一下各位老师的积分吗? Input 多组测试数据, ...

  6. euclidea 3.0 全三星 攻略

    euclidea攻略 游戏地址 http://www.euclidea.xyz/en/game/#/packs 攻略 Alpha level : 1.1 line tool 3L3E 智障题 1.2 ...

  7. ZOJ 2969 Easy Task

    E - Easy Task Description Calculating the derivation of a polynomial is an easy task. Given a functi ...

  8. SGU 406 Goggle

    406. Goggle Time limit per test: 0.25 second(s)Memory limit: 65536 kilobytes input: standardoutput: ...

  9. 在当前的webview中跳转到新的url 使用WebView组件显示网页

    如果希望点击链接由自己处理,而不是新开Android的系统browser中响应该链接.给WebView加一个事件监听对象(WebViewClient)并重写其中的一些方法:shouldOverride ...

  10. Newtonsoft.Json报错:未能加载文件或程序集"..."或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配

    Newtonsoft.Json报错:未能加载文件或程序集"..."或它的某一个依赖项.找到的程序集清单定义与程序集引用不匹配.   □ 背景分析 在帮助类库中使用了Newtonso ...