我们看看NSFileManager如何使用。包括创建文件,目录,删除,遍历目录等。

1、在Documents里创建目录

创建一个叫test的目录,先找到Documents的目录,

  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2. NSString *documentsDirectory = [paths objectAtIndex:0];
  3. NSLog(@"documentsDirectory%@",documentsDirectory);
  4. NSFileManager *fileManager = [NSFileManager defaultManager];
  5. NSString *testDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];
  6. // 创建目录
  7. [fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];

启动程序,这时候目录就创建了:

2、在test目录下创建文件

创建文件怎么办呢?接着上面的代码 testPath 要用stringByAppendingPathComponent拼接上你要生成的文件名,比如test00.txt。这样才能在test下写入文件。

testDirectory是上面代码生成的路径哦,不要忘了。我往test文件夹里写入三个文件,test00.txt ,test22.txt,text.33.txt。内容都是写入内容,write String。

实现代码如下:

  1. NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test00.txt"];
  2. NSString *testPath2 = [testDirectory stringByAppendingPathComponent:@"test22.txt"];
  3. NSString *testPath3 = [testDirectory stringByAppendingPathComponent:@"test33.txt"];
  4. NSString *string = @"写入内容,write String";
  5. [fileManager createFileAtPath:testPath contents:[string  dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
  6. [fileManager createFileAtPath:testPath2 contents:[string  dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
  7. [fileManager createFileAtPath:testPath3 contents:[string  dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

看下面的图,三个文件都出来了,内容也对。

在Documents目录下创建就更简单了,不用加test就ok了

3、获取目录列里所有文件名

两种方法获取:subpathsOfDirectoryAtPath 和 subpathsAtPath

  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2. NSString *documentsDirectory = [paths objectAtIndex:0];
  3. NSLog(@"documentsDirectory%@",documentsDirectory);
  4. NSFileManager *fileManage = [NSFileManager defaultManager];
  5. NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];
  6. NSArray *file = [fileManage subpathsOfDirectoryAtPath: myDirectory error:nil];
  7. NSLog(@"%@",file);
  8. NSArray *files = [fileManage subpathsAtPath: myDirectory ];
  9. NSLog(@"%@",files);

获取上面刚才test文件夹里的文件名

打印结果

2012-06-17 23:23:19.684 IosSandbox[947:f803] fileList:(

    ".DS_Store",

    "test00.txt",

    "test22.txt",

    "test33.txt"

)

2012-06-17 23:23:19.686 IosSandbox[947:f803] fileLit(

    ".DS_Store",

    "test00.txt",

    "test22.txt",

    "test33.txt"

)

两个方法都可以,隐藏的文件也打印出来了。

4、fileManager使用操作当前目录

  1. //创建文件管理器
  2. NSFileManager *fileManager = [NSFileManager defaultManager];
  3. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  4. NSString *documentsDirectory = [paths objectAtIndex:0];
  5. //更改到待操作的目录下
  6. [fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
  7. //创建文件fileName文件名称,contents文件的内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil
  8. NSString * fileName = @"testFileNSFileManager.txt";
  9. NSArray *array = [[NSArray alloc] initWithObjects:@"hello world",@"hello world1", @"hello world2",nil];
  10. [fileManager createFileAtPath:fileName contents:array attributes:nil];

这样就创建了testFileNSFileManager.txt并把三个hello world写入文件了

changeCurrentDirectoryPath目录更改到当前操作目录时,做文件读写就很方便了,不用加上全路径

5、删除文件

接上面的代码,remove就ok了。

  1. [fileManager removeItemAtPath:fileName error:nil];

6、混合数据的读写

用NSMutableData创建混合数据,然后写到文件里。并按数据的类型把数据读出来

6.1写入数据:

  1. NSString * fileName = @"testFileNSFileManager.txt";
  2. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  3. NSString *documentsDirectory = [paths objectAtIndex:0];
  4. //获取文件路径
  5. NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];
  6. //待写入的数据
  7. NSString *temp = @"nihao 世界";
  8. int dataInt = 1234;
  9. float dataFloat = 3.14f;
  10. //创建数据缓冲
  11. NSMutableData *writer = [[NSMutableData alloc] init];
  12. //将字符串添加到缓冲中
  13. [writer appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]];
  14. //将其他数据添加到缓冲中
  15. [writer appendBytes:&dataInt length:sizeof(dataInt)];
  16. [writer appendBytes:&dataFloat length:sizeof(dataFloat)];
  17. //将缓冲的数据写入到文件中
  18. [writer writeToFile:path atomically:YES];

我们看看数据怎么样了:



我们看到后面的是乱码,那是中文被转成了NSData后,还有int float的二进制

6.2读取刚才写入的数据:

  1. //读取数据:
  2. int intData;
  3. float floatData = 0.0;
  4. NSString *stringData;
  5. NSData *reader = [NSData dataWithContentsOfFile:path];
  6. stringData = [[NSString alloc] initWithData:[reader subdataWithRange:NSMakeRange(0, [temp length])]
  7. encoding:NSUTF8StringEncoding];
  8. [reader getBytes:&intData range:NSMakeRange([temp length], sizeof(intData))];
  9. [reader getBytes:&floatData range:NSMakeRange([temp length] + sizeof(intData), sizeof(floatData))];
  10. NSLog(@"stringData:%@ intData:%d floatData:%f", stringData, intData, floatData);

打印出来的结果:

2012-06-17 23:51:14.723 IosSandbox[1285:f803] stringData:nihao hello! intData:1234332 floatData:3.140000

这里把写入的汉字改成了 hello。因为[temp length]算长度是,把中文算成一位了,出来的结果有误。

例子代码:https://github.com/schelling/YcDemo

著作权声明:本文由http://blog.csdn.net/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢

iOS学习之iOS沙盒(sandbox)机制和文件操作之NSFileManager的更多相关文章

  1. iOS学习之iOS沙盒(sandbox)机制和文件操作之NSFileManager(三)

    1.在Documents里创建目录 创建一个叫test的目录,先找到Documents的目录, NSArray *paths = NSSearchPathForDirectoriesInDomains ...

  2. IOS学习之IOS沙盒(sandbox)机制和文件操作

    IOS学习之IOS沙盒(sandbox)机制和文件操作(一) 1.IOS沙盒机制 IOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都 ...

  3. IOS沙盒(sandbox)机制和文件操作

    IOS学习之IOS沙盒(sandbox)机制和文件操作   作者:totogo2010 ,发布于2012-9-21,来源:CSDN   目录: IOS学习之IOS沙盒(sandbox)机制和文件操作( ...

  4. iOS学习之iOS沙盒(sandbox)机制和文件操作1

    iOS学习之iOS沙盒(sandbox)机制和文件操作 接上篇 iOS学习之iOS沙盒(sandbox)机制和文件操作(一) 我们看看如何获取应用程序沙盒目录.包括真机的沙盒的目录. 1.获取程序的H ...

  5. iOS学习之iOS沙盒(sandbox)机制和文件操作(一)

    1.iOS沙盒机制 iOS应用程序仅仅能在为该改程序创建的文件系统中读取文件,不能够去其他地方訪问,此区域被成为沙盒,所以全部的非代码文件都要保存在此,比如图像,图标,声音,映像,属性列表,文本文件等 ...

  6. iOS学习之iOS沙盒(sandbox)机制和文件操作复习

    1.iOS沙盒机制 iOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等. ...

  7. iOS学习之iOS沙盒(sandbox)机制和文件操作(二)

    1.获取程序的Home目录 NSString *homeDirectory = NSHomeDirectory(); NSLog(@"path:%@", homeDirectory ...

  8. iOS 沙盒(sandbox)机制和文件操作

    本文参看了 http://www.uml.org.cn/mobiledev/201209211.asp#1 这篇文章中的介绍,尊重原著. 1.IOS沙盒机制 IOS应用程序只能在本应用程序中创建的文件 ...

  9. iOS学习7:iOS沙盒(sandBox)机制(一)之获取沙盒路径及目录说明(转)

    转:http://my.oschina.net/joanfen/blog/151145 一.iOS沙盒机制 iOS的应用只能访问为该应用创建的区域,不可访问其他区域,应用的其他非代码文件都存在此目录下 ...

随机推荐

  1. 【angularJS】Filter 过滤器

    当从后台获取到的数据呈现到视图上时,此时可能需要对数据进行相应的转换,此时我们可以通过过滤器在不同页面进行不同数据的格式抓换,在AngularJS中有常见默认的过滤器,当然若不满足所需,我们可以自定义 ...

  2. vue 中import和export如何一起使用(一)

    前段时间碰到一个问题,vue js中要使用import来加载第三方的js,但是后面使用exports.XXX的话会报exports is not defined.那要怎么解决呢? 首先,我们要了解ES ...

  3. php mysql 字符集(三) (转)

    http://bbs.csdn.net/topics/390097514 gbk页面插入数据到utf8表,然后取出到gbk页面 首先, 这个set names x等价于SET character_se ...

  4. bzoj 2039 [2009国家集训队]employ人员雇佣——二元关系

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2039 用最小割看.对于一组关系 i , j ,如果都选,收益 2*Ei,j,可以看作0,作为 ...

  5. 11.Python使用Scrapy爬虫小Demo(新手入门)

    1.前提:已安装好scrapy,且已新建好项目,编写小Demo去获取美剧天堂的电影标题名 2.在项目中创建一个python文件 3.代码如下所示: import scrapy class movies ...

  6. 分享百度文件上传组件webUploader的使用demo

    先创建DOM节点:<head ng-app="myApp"> <meta charset="UTF-8"> <title>& ...

  7. undefined vs. null

    undefined vs. null 一.相似性 在JavaScript中,将一个变量赋值为undefined或null,老实说,几乎没区别. var a = undefined; var a = n ...

  8. hyperledger fabric共识组件分析 --背书策略

    在fabric中,共识过程意味着多个节点对于某一批交易的发生顺序.合法性以及它们对账本状态的更新结构达成一致的观点.满足共识则意味着多个节点可以始终保证相同的状态,对于以同样顺序到达的交易可以进行一致 ...

  9. WinForm界面布局控件WeifenLuo.WinFormsUI.Docking"的使用 (一)

    WinForm界面布局控件WeifenLuo.WinFormsUI.Docking"的使用 (一) 编写人:CC阿爸 2015-1-28 在伍华聪的博客中,看到布局控件"Weife ...

  10. 解决近期linux下yum更新出现HTTP Error 404 NOT FOUND错误的办法

    本文转载自:http://tech.lezi.com/archives/47 最近两天使用yum的163源,出现404错误 [root@localhost yum.repos.d]# yum make ...