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. 基于EfCore的一个多租户Demo

    六月份如愿的转入了架构组,先上手搞了个CI服务器,把架构组的几个项目撸到上面去了.效果不错,接着就把其他两个项目组有单元测试的项目撸上去了,在桌子上放了个显示器当大屏用. 因为公司准备进行一个大的系统 ...

  2. 手机pc显示不同的内容

    <script type="text/javascript"> // var txt = $('#sjyincang').html(); // alert(txt); ...

  3. 官网下载kettle

    首先什么是kettle,引用下百度百科 Kettle是一款国外开源的ETL工具,纯java编写,可以在Window.Linux.Unix上运行,数据抽取高效稳定. Kettle 中文名称叫水壶,该项目 ...

  4. kubernetes-notes--阅读笔记

    文档地址 https://www.huweihuang.com/kubernetes-notes/

  5. aop学习总结三----aop的相关概念

    aop学习总结三----aop的相关概念 public Object invoke(Object proxy, Method method, Object[] args) throws Throwab ...

  6. android开发基础知识了解

    JDK下载:www.oracle.com; SDK下载:www.android.developer.com; eclipse下载:www.eclipse.org;

  7. php钩子技术

    通过对钩子技术的理解,下面把自己对钩子技术的总结如下:"钩子就是在调用php核心功能代码的同时可以在不修改核心代码的同时,更改核心代码的行为";具体模拟实现如下 /** * php ...

  8. 使用doctrine的内存耗尽解决办法

    PHP Fatal error: Allowed memory size of xxx xxx xxx bytes exhausted 无论是插入大量数据或者查询大量数据时,都可能因为数据量太大而出现 ...

  9. DHCP request error:Timed out waiting for dhcpcd to start【转】

    本文转载自:http://blog.csdn.net/zvivi521/article/details/9166899 [init.svc.dhcpcd_eth0]: [stopped] I/Serv ...

  10. codeforces 509 B题 Painting Pebbles

    转载地址:http://blog.csdn.net/nike0good/article/details/43449739 B. Painting Pebbles time limit per test ...