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:

最新文章

  1. 放弃安卓原生TimePicker,选择wheelView打造更漂亮的时间get,以及动态拉伸输入框布局,这些,这里都有!
  2. php namespace用法
  3. Android之登录时密码的保护
  4. Asp.Net MVC中DropDownListFor的用法
  5. HDU 1517 (类巴什博奕) A Multiplication Game
  6. taglist
  7. MFC——AfxParseURL用法
  8. 快速安装VIM开发环境
  9. jQuery源码笔记——准备
  10. Immediate Decodability问题Java解答
  11. Android音频焦点详解(上)
  12. RabbitMQ入门-从HelloWorld开始
  13. Bzoj2442:修剪草坪
  14. Java - "JUC" CountDownLatch源码分析
  15. 【LeetCode】136. Single Number (4 solutions)
  16. Intellij Idea使用及配置
  17. 5分钟教你配置命令行界面的163邮箱client
  18. 负载均衡,会话保持,session同步
  19. php核心技术与最佳实践知识点(上)
  20. Github加载慢,显示不完整问题解决

热门文章

  1. HBase写请求分析
  2. Linux 中权限的再讨论( 下 )
  3. RDLC报表 报表数据 栏 快捷键
  4. Python数据分析简介
  5. Bubble Cup X - Finals [Online Mirror] B. Neural Network country 矩阵快速幂加速转移
  6. 在Eclipse中建立Maven Web项目
  7. asp.net mvc4 修改密码界面
  8. [自动化平台系列] - 初次使用 Macaca-前端自动化测试(2)
  9. git推送已有项目到gitee
  10. wireshark 学习 2