NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *str1 = NSHomeDirectory();
_filePath = [[NSString stringWithFormat:@"%@/Documents/imageViews/test.plist",str1]retain]; NSLog(@"%@",_filePath);
if(![fileManager fileExistsAtPath:_filePath]){//如果不存在,则说明是第一次运行这个程序,那么建立这个文件夹
NSLog(@"first run");
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *directryPath = [path stringByAppendingPathComponent:@"imageViews"];
[fileManager createDirectoryAtPath:directryPath withIntermediateDirectories:YES attributes:nil error:nil];
NSString *filePath = [directryPath stringByAppendingPathComponent:@"test.plist"];
NSLog(@"%@",filePath);
[fileManager createFileAtPath:filePath contents:nil attributes:nil];
.........
}

此段代码创建的filePath为

/Users/yuqiu/Library/Application Support/iPhone Simulator/6.1/Applications/2A8EFE9E-4CCA-41D1-8A5F-1FF00A115FA3/Documents/imageViews/test.plist
 

一、创建~/Documents/imageViews/test.plist的详细步骤

1、找到Documetnts目录所在的位置

NSString *str1 = NSHomeDirectory();

str1为/Users/yuqiu/Library/Application Support/iPhone Simulator/6.1/Applications/2A8EFE9E-4CCA-41D1-8A5F-1FF00A115FA3

2、加上Documetnts这层目录

 NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

path为/Users/yuqiu/Library/Application Support/iPhone Simulator/6.1/Applications/2A8EFE9E-4CCA-41D1-8A5F-1FF00A115FA3/Documents
注意:使用的是stringByAppendingPathComponent:方法,不需要加“/";  如果用的是别的方法加的话,需要写成@"/Documents".

ps:这里1、2两步可以简化为

NSArray *paths = NSSearchPathForDerictoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path = [paths lastObject];

3、在Documetnts目录中创建名为imageViews的目录

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *directryPath = [path stringByAppendingPathComponent:@"imageViews"];
[fileManager createDirectoryAtPath:directryPath withIntermediateDirectories:YES attributes:nil error:nil];

创建NSFileManager的实例fileManager.NSFileManager这个类是专门进行文件管理的,可以创建文件,目录,删除,遍历目录等。我们调用了 createDirectoryAtPath:方法创建了一个新目录。

4、在imageViews目录里,创建文件test.plist.

NSString *filePath = [directryPath stringByAppendingPathComponent:@"test.plist"];
[fileManager createFileAtPath:filePath contents:nil attributes:nil];

调用createFileAtPath:创建文件
   最后得到的整个文件路径为:

二、NSFileManager常用的文件管理操作

1、创建目录  createDirectoryAtPath:

2、创建文件  createFileAtPath:

3、删除某个文件  removeItemAtPath:

4、检查某个文件是否存在  fileExistsAtPath:

5、检查文件是否可读 isReadableFileAtPath:

是否可写:isWritableFileAtPath:

6、取得文件属性  fileAttributesAtPath:

改变文件属性changeAttributesAtPath:

7、从path代表的文件中读取数据:contentsAtPath

8、移动文件movePath:toPath:handler:

9、复制文件copyPath:toPath:handler:

ios NSFileManager创建目录、文件的更多相关文章

  1. IOS 创建目录/文件夹

    •IOS 应用目录简介 个文件夹:Documents, Library和 tmp.Library包含Caches.Preferences目录. Documents:应用中用户数据可以放在这里,iTun ...

  2. iOS NSFileManager 使用详解

    使用NSFileManager 文件系统接口 允许访问文件夹内容 创建 重命名 删除文件 修改文件和文件属性,以及Finder对所有文件系统任务执行的一般操作. 访问NSFileManager,使用共 ...

  3. iOS开发中文件的上传和下载功能的基本实现-备用

    感谢大神分享 这篇文章主要介绍了iOS开发中文件的上传和下载功能的基本实现,并且下载方面讲到了大文件的多线程断点下载,需要的朋友可以参考下 文件的上传 说明:文件上传使用的时POST请求,通常把要上传 ...

  4. iOS 工程中文件变成红色是什么情况

    iOS 工程中文件变成红色是原有的文件路径改变了,系统找不到了.

  5. iOS的常见文件及程序的启动原理

    一. iOS中常见文件 (一). Xcode6之前 创建项目,默认可以看见一个存放框架的文件夹 info文件以工程文件名开头,如:第一个项目-Info.plist 项目中默认有一个PCH文件 (二). ...

  6. iOS Dev (22) 文件、路径

    iOS Dev (22) 文件.路径 作者:CSDN 大锐哥 博客:http://blog.csdn.net/prevention 沙箱 Sandbox 的路径 和其他很多应用平台一样,iOS 也限定 ...

  7. IOS上传文件开发

    IOS上传文件开发     在移动应用开发  文件形式上传是不可缺少的,近期把IOS这块文件上传文件代码简单的整理一下.假设大家有须要安卓这边的代码,本人也能够分享给大家! QQ群:74432915 ...

  8. iOS开发-plist文件增删改查

    plist第一次看到这个后缀名文件的时候感觉怪怪的,不过接触久了也就习以为常了,plist是Property List的简称可以理解成属性列表文件,主要用来存储串行化后的对象的文件.扩展名为.plis ...

  9. iOS 苹果官方 Crash文件分析方法 (iOS系统Crash文件分析方法)

    时间2013-08-20 12:49:20 GoWhich原文  http://www.gowhich.com/blog/view/id/343 苹果官方 Crash文件分析方法 (iOS系统Cras ...

随机推荐

  1. 如何在微信小程序中使用字体图标

    微信小程序中,在image标签里,可以在src中引用本地文件,但是background设置背景图或者使用字体图标的时候,却不能引用本地文件,只能用url地址的图片或字体,或者使用base64编码后的格 ...

  2. repeter中应用三元运算符

    应用情景一:根据ID显示名称例如:0代表启动,1:代表关闭例子如下 <td><%#Eval("ID").ToString() == "0" ? ...

  3. 设置netbeans文件编码格式

    在项目ecmall上右键 选择属性,然后在项目属性里设置

  4. Xcode 6 IDE

    本文转载至 http://www.cocoachina.com/ios/20140823/9442.html     (via:苹果开发者中心) Xcode IDE 是 Apple 开发体验的核心.X ...

  5. 关于Darwin接入私有协议、私有SDK码流的讨论

          最近做到云视频/云监控的项目,跟团队伙伴讨论到一个架构问题,就是将私有协议的码流数据接入到Darwin,再通过Darwin对外提供高效的RTSP/RTP服务.说到私有协议接入Darwin, ...

  6. EasyDarwin开源流媒体服务器将select改为epoll的方法

    本文来自EasyDarwin团队Fantasy(fantasy(at)easydarwin.org) 一. EasyDarwin网络模型介绍 EventContext负责监听所有网络读写事件,Even ...

  7. 2018.11.23-day27 面向对象(大总结)

    1. 2个内置函数 2.反射 3.内置方法 4.面向对象大总结 思维导图地址:https://www.processon.com/view/link/5bfcfd62e4b0ef094cbf04a7  ...

  8. hsv hsb rgb lab

    lab  欧式距离 反映 人类所能感受到的这两种颜色的差异

  9. npm ERR! Unexpected end of JSON input while parsing near '...inimist":"^1.2.0"}

    简介 在项目中执行npm install安装依赖包的时候.出现npm ERR! Unexpected end of JSON input while parsing near '...inimist& ...

  10. python错误提示“TabError: inconsistent use of tabs and spaces in indentation”

    在遍历打印10以内的奇数是出现“TabError: inconsistent use of tabs and spaces in indentation”的错误提示: 代码如下: 第一感觉没什么错误, ...