ios NSFileManager创建目录、文件
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为
一、创建~/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创建目录、文件的更多相关文章
- IOS 创建目录/文件夹
•IOS 应用目录简介 个文件夹:Documents, Library和 tmp.Library包含Caches.Preferences目录. Documents:应用中用户数据可以放在这里,iTun ...
- iOS NSFileManager 使用详解
使用NSFileManager 文件系统接口 允许访问文件夹内容 创建 重命名 删除文件 修改文件和文件属性,以及Finder对所有文件系统任务执行的一般操作. 访问NSFileManager,使用共 ...
- iOS开发中文件的上传和下载功能的基本实现-备用
感谢大神分享 这篇文章主要介绍了iOS开发中文件的上传和下载功能的基本实现,并且下载方面讲到了大文件的多线程断点下载,需要的朋友可以参考下 文件的上传 说明:文件上传使用的时POST请求,通常把要上传 ...
- iOS 工程中文件变成红色是什么情况
iOS 工程中文件变成红色是原有的文件路径改变了,系统找不到了.
- iOS的常见文件及程序的启动原理
一. iOS中常见文件 (一). Xcode6之前 创建项目,默认可以看见一个存放框架的文件夹 info文件以工程文件名开头,如:第一个项目-Info.plist 项目中默认有一个PCH文件 (二). ...
- iOS Dev (22) 文件、路径
iOS Dev (22) 文件.路径 作者:CSDN 大锐哥 博客:http://blog.csdn.net/prevention 沙箱 Sandbox 的路径 和其他很多应用平台一样,iOS 也限定 ...
- IOS上传文件开发
IOS上传文件开发 在移动应用开发 文件形式上传是不可缺少的,近期把IOS这块文件上传文件代码简单的整理一下.假设大家有须要安卓这边的代码,本人也能够分享给大家! QQ群:74432915 ...
- iOS开发-plist文件增删改查
plist第一次看到这个后缀名文件的时候感觉怪怪的,不过接触久了也就习以为常了,plist是Property List的简称可以理解成属性列表文件,主要用来存储串行化后的对象的文件.扩展名为.plis ...
- iOS 苹果官方 Crash文件分析方法 (iOS系统Crash文件分析方法)
时间2013-08-20 12:49:20 GoWhich原文 http://www.gowhich.com/blog/view/id/343 苹果官方 Crash文件分析方法 (iOS系统Cras ...
随机推荐
- Spring mvc接受集合类型参数的方法
public String xxxxx(String xxxx, String xxxxx, @RequestParam("parameterList[]") List<St ...
- r testifying that your code will behave as you intend.
https://github.com/stretchr/testify Testify - Thou Shalt Write Tests Go code (golang) set of pack ...
- extjs grid renderer参数用法
今天在导出EXT的二维时老是报错,追进去看是renderer : function(value)的参数不对,经过一番研究,未免以后遇到再次浪费时间,记录一下. var cm = new Ext.gri ...
- jquery特效(1)—点击展示与隐藏全文
下班了~~~我把今天整理的一个jquery小特效发一下,个人觉得比较简单,嗖嗖的就写出来了~~~ 下面先来看最终的动态效果: 一.来看一下主体框架程序: <!DOCTYPE html> & ...
- POJ2478 Farey Sequence —— 欧拉函数
题目链接:https://vjudge.net/problem/POJ-2478 Farey Sequence Time Limit: 1000MS Memory Limit: 65536K To ...
- hdu-4990 Reading comprehension(快速幂+乘法逆元)
题目链接: Reading comprehension Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- 「LuoguP3379」 【模板】最近公共祖先(LCA)
题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询问的个数和树根结点的序号. 接下来N-1行每 ...
- 使用 @RequestMapping 映射请求
- 增加,删除GMS包
1. device/hiteq/vtab_1050_standard/httek.mk BUILD_GMS:=yes GMS_VARIANT:=mini 2. rm out/target/produc ...
- vijos1842(火柴排队)
描述 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为:∑i=1n(ai−bi)2∑i=1n(ai−bi) ...