NSFileManager的简单介绍,在沙盒目录下对文件进行增删改查
2015-10-28 22:42
316 查看
ios应用程序中所产生的所有资源和数据都存放在它的沙盒目录下,沙盒目录中主要包含三个文件夹,
沙盒路径的获取:
Documents:将程序创建产生的文件以及应用浏览产生的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录
Library:
Caches:存放缓存文件,保持数据的持久化
Preferences:偏好设置
tmp:存放零时文件,重启时删除
2.通过字符串来创建文件,并将字符串写入相应的文件中
3.在沙盒下移动文件
4.沙盒下拷贝文件
5.在沙盒下删除文件
6.在沙盒下查找文件
7.在沙盒下写入数据
沙盒路径的获取:
NSLog(@"%@",NSHomeDirectory());//打印出相关路径,在前往文件夹下达到
Documents:将程序创建产生的文件以及应用浏览产生的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录
//到达Documents文件夹下 NSArray *storeFile = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docunmentDirectory = [storeFile objectAtIndex:0];
Library:
Caches:存放缓存文件,保持数据的持久化
Preferences:偏好设置
//到达Library目录下 NSArray *librarypaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *libraryDirectory = [librarypaths objectAtIndex:0];
//到达缓存目录下 NSArray *cache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cachePath = [cache objectAtIndex:0];
tmp:存放零时文件,重启时删除
//到达tmp目录下 NSString *tmpDirectory = NSTemporaryDirectory();
NSFileManager的使用:
1.使用NSFileManager创建文件,在沙盒路径下创建一个想要的文件和文件夹
//初始化NSFileManager NSFileManager *manager = [NSFileManager defaultManager]; //创建文件夹相关路径(在document文件夹下创建) NSString *filepath = [docunmentDirectory stringByAppendingPathComponent:@"moxue.plist"]; [manager createFileAtPath:filepath contents:nil attributes:nil]; //创建moxue文件夹 NSString *documentpath = [docunmentDirectory stringByAppendingPathComponent:@"moxue"]; [manager createFileAtPath:documentpath contents:nil attributes:nil]; //使用NSFileManager创建文件 //创建文件的属性为attributes,可通过attributesOfItemAtPath中的属性列表查看,对attributes传入相关的字典就可以更改了。 //通过attributesOfItemAtPath中的属性列表查看 // NSLog(@"%@",[manager attributesOfItemAtPath:filepath error:nil]);
2.通过字符串来创建文件,并将字符串写入相应的文件中
//通过字符串来创建文件,将字符串word写入filepath2文件路径中 NSString *word = @"墨雪"; NSString *filepath2 = [NSHomeDirectory() stringByAppendingPathComponent:@"moxue1"]; [word writeToFile:filepath2 atomically:YES encoding:NSUTF8StringEncoding error:nil];
3.在沙盒下移动文件
//移动文件 NSString *destation = [[NSHomeDirectory() stringByAppendingPathComponent:@"moxue"] stringByAppendingPathComponent:@"moxue2.plist"]; [manager moveItemAtPath:filepath2 toPath:destation error:nil];
4.沙盒下拷贝文件
//拷贝文件 NSString *copypath = [[NSHomeDirectory() stringByAppendingPathComponent:@"moxue"]stringByAppendingPathComponent:@"copy.plist"]; [manager copyItemAtPath:destation toPath:copypath error:nil];
5.在沙盒下删除文件
//删除文件 [manager removeItemAtPath:copypath error:nil];
6.在沙盒下查找文件
//查找文件,得到一个数组,得到数组中所有的数据 NSArray *array = [manager contentsOfDirectoryAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"moxue"] error:nil]; NSLog(@"array = %@",array);
7.在沙盒下写入数据
//在文件中写入数据 NSDictionary *dict2 = [NSDictionary dictionaryWithObject:@"dsada122" forKey:@"item12"]; [dict2 writeToFile:destation atomically:YES]; NSDictionary *dict22 = [NSDictionary dictionaryWithContentsOfFile:destation];
相关文章推荐
- IOS--之UIWindow(窗口)、UIView(视图)
- DP-POJ-3666-Making the Grade
- C#编程--运算符
- 12、json、GridView、缓存
- Unix命令行程序和内建指令(更多)
- Android Scrollview+Listview 实现不同条目点赞并计数功能及原理分析(1)
- 京都的秋
- noip 10.28模拟
- 11.webview、shareSDK
- 项目冲刺--第一天
- POJ 3295 递归模拟
- Linux 查看PCI-E插槽使用情况
- Notepad++ 快捷键 大全
- 理解正向代理与方向代理
- 10.下拉刷新、加载更多、标记已读、轮播条、缓存
- Activity和Service之间的交互
- Rebranding(小写字母规律题)
- metasploit渗透测试(一)在 Mac 安装 Metasploit-framework
- java大数加法
- 项目40.2 对于三位数字,若各位数字立方和等于该数,该数就是水仙花数。如153:153=13+53+33