IOS 开发文件操作——NSFileManager
转自:http://blog.csdn.net/xyz_lmn/article/details/8968213,留着方便查阅
iOS的沙盒机制,应用只能访问自己应用目录下的文件。iOS不像android,没有SD卡概念,不能直接访问图像、视频等内容。iOS应用产生的内容,如图像、文件、缓存内容等都必须存储在自己的沙盒内。默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp。Library包含Caches、Preferences目录。
上面的完整路径为:用户->资源库->Application Support->iPhone Simulator->6.1->Aplications
iPhone会为每一个应用程序生成一个私有目录,这个目录位于:
/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications下,
并随即生成一个数字字母串作为目录名,在每一次应用程序启动时,这个字母数字串都是不同于上一次。
Documents:苹果建议将程序创建产生的文件以及应用浏览产生的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录
Library:存储程序的默认设置或其它状态信息;
Library/Caches:存放缓存文件,保存应用的持久化数据,用于应用升级或者应用关闭后的数据保存,不会被itunes同步,所以为了减少同步的时间,可以考虑将一些比较大的文件而又不需要备份的文件放到这个目录下。
tmp:提供一个即时创建临时文件的地方,但不需要持久化,在应用关闭后,该目录下的数据将删除,也可能系统在程序不运行的时候清除。
APP Sandbox
iOS怎么获取沙盒路径,怎么操作文件呢?下面给出答案。
获取应用沙盒根路径:
- -(void)dirHome{
- NSString *dirHome=NSHomeDirectory();
- NSLog(@"app_home: %@",dirHome);
- }
获取Documents目录路径:
[cpp] view plaincopyprint?
- //获取Documents目录
- -(NSString *)dirDoc{
- //[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- NSLog(@"app_home_doc: %@",documentsDirectory);
- return documentsDirectory;
- }
获取Library目录路径:
[cpp] view plaincopyprint?
- //获取Library目录
- -(void)dirLib{
- //[NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
- NSString *libraryDirectory = [paths objectAtIndex:0];
- NSLog(@"app_home_lib: %@",libraryDirectory);
- }
获取Cache目录路径:
- //获取Cache目录
- -(void)dirCache{
- NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
- NSString *cachePath = [cacPath objectAtIndex:0];
- NSLog(@"app_home_lib_cache: %@",cachePath);
- }
获取Tmp目录路径:
- //获取Tmp目录
- -(void)dirTmp{
- //[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];
- NSString *tmpDirectory = NSTemporaryDirectory();
- NSLog(@"app_home_tmp: %@",tmpDirectory);
- }
创建文件夹:
withIntermediateDirectories:YES 如果文件夹不存在,则创建, 如果存在表示可以覆盖 NO 如果文件夹不存在,则创建, 如果存在不可以覆盖
- //创建文件夹
- -(void *)createDir{
- NSString *documentsPath =[self dirDoc];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
- // 创建目录
- BOOL res=[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
- if (res) {
- NSLog(@"文件夹创建成功");
- }else
- NSLog(@"文件夹创建失败");
- }
创建文件
- //创建文件
- -(void *)createFile{
- NSString *documentsPath =[self dirDoc];
- NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
- BOOL res=[fileManager createFileAtPath:testPath contents:nil attributes:nil];
- if (res) {
- NSLog(@"文件创建成功: %@" ,testPath);
- }else
- NSLog(@"文件创建失败");
- }
写数据到文件:
- //写文件
- -(void)writeFile{
- NSString *documentsPath =[self dirDoc];
- NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
- NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
- NSString *content=@"测试写入内容!";
- BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
- if (res) {
- NSLog(@"文件写入成功");
- }else
- NSLog(@"文件写入失败");
- }
读文件数据:
- //读文件
- -(void)readFile{
- NSString *documentsPath =[self dirDoc];
- NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
- NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
- // NSData *data = [NSData dataWithContentsOfFile:testPath];
- // NSLog(@"文件读取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
- NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil];
- NSLog(@"文件读取成功: %@",content);
- }
文件属性:
- //文件属性
- -(void)fileAttriutes{
- NSString *documentsPath =[self dirDoc];
- NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
- NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:testPath error:nil];
- NSArray *keys;
- id key, value;
- keys = [fileAttributes allKeys];
- int count = [keys count];
- for (int i = 0; i < count; i++)
- {
- key = [keys objectAtIndex: i];
- value = [fileAttributes objectForKey: key];
- NSLog (@"Key: %@ for value: %@", key, value);
- }
- }
删除文件:
- //删除文件
- -(void)deleteFile{
- NSString *documentsPath =[self dirDoc];
- NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
- BOOL res=[fileManager removeItemAtPath:testPath error:nil];
- if (res) {
- NSLog(@"文件删除成功");
- }else
- NSLog(@"文件删除失败");
- NSLog(@"文件是否存在: %@",[fileManager isExecutableFileAtPath:testPath]?@"YES":@"NO");
- }
IOS 开发文件操作——NSFileManager的更多相关文章
- iOS开发-文件操作
目录操作和文件管理 学习目标 1.理解单例 2.掌握NSFileManager类常用的文件管理操 3.掌握NSFileHandle类常用的文件数据操作 4.了解NSData类的常用操作 5.掌握Pli ...
- iOS 关于文件操作 NSFileManager
创建文件夹 + (BOOL)creatDir:(NSString *)path { if (path.length==0) { return NO; } NSFileManager *fileMana ...
- iOS——文件操作NSFileManager (创建、删除,复制,粘贴)
iOS——文件操作NSFileManager (创建.删除,复制,粘贴) iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视 ...
- iOS:文件操作相关(18-03-23更)
0.iOS文件系统 1.工程内文件 2.文件夹管理 3.文件操作 4.NSCache 附录: 1.沙盒文件夹.文件大小 2.清除沙盒 Library / Cache 下所有数据 3.测试plist 0 ...
- iOS文件和目录操作,iOS文件操作,NSFileManager使用文件操作:
NSFileManager常用的文件方法: -(NSData*)contentsAtPath:path 从一个文件中读取数据 -(BOLL)createFileAtPath:path contents ...
- Objective-C文件和目录操作,IOS文件操作,NSFileManager使用文件操作
http://blog.csdn.net/swingpyzf/article/details/15185767
- 文件操作 - NSFileManager
iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视频等内容.iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙盒内.默认 ...
- ios 关于文件操作 获取 文件大小
分类: Apple IPhone2012-06-28 11:31 4664人阅读 评论(0) 收藏 举报 ios语言manager测试c c语言 实现 #include "sys/stat ...
- iOS 的文件操作
直接上操作 效果:将一张图片写入文件 (图片本身已经在Assets.xcassets里面了) 1.获取当前app的沙盒路径 NSString *documentPath = NSSearchPathF ...
随机推荐
- ios cocos2d FPS过低的解决方法
每当运行程序时,左下角的FPS就低到了10,使app很卡, 原来程序主要卡的部分 -(void)draw{ NSDate *startTime = [NSDate date]; [self func] ...
- 常用移动web开发框架研究分析
纯粹的总结一下移动web开发框架,移动web开发框架有jQuery Mobile .Sencha Touch等等,他们都来源于web开发,是成熟的框架,jQuery Mobile出自于jQuery家族 ...
- 【BZOJ】3038: 上帝造题的七分钟2(线段树+暴力)
http://www.lydsy.com:808/JudgeOnline/problem.php?id=3038 这题我就有得吐槽了,先是线段树更新写错,然后不知哪没pushup导致te,精度问题sq ...
- Dijkstra堆优化与SPFA模板
Dijkstra+优先队列 #include<cstdio> #include<cctype> #include<queue> #include<cstrin ...
- JS按位非(~)运算符与~~运算符的理解分析
按位非运算符,简单的理解就是改变运算数的符号并减去1,当然,这是只是简单的理解能转换成number类型的数据. 那么,对于typeof var!==”number”的类型来说,进行运算时,会尝试转化成 ...
- C#SortedList排序列表怎么样逆序输出
C#分别在集合库和泛型库中有共2套SortedList 以较新的泛型库为例,SortedList<int, string> l = new SortedList<int, strin ...
- mac使用指南:brew的安装
官网 http://brew.sh/ 安装并且下载brew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/ ...
- [转]C# WinForm动态调用远程Web服务
本文转自:http://blog.csdn.net/muyangjun/article/details/7930871 1.添加服务引用 2.在弹出的添加服务引用对话框地址栏中输入WebService ...
- centeros iptable模板文件
iptables规则是空的.而且他们的selinux是关闭了的,这等同于把系统裸奔(总比windows裸奔好). 使用方法: 1.用root用户登录后 vi /etc/sysconfig/ipta ...
- JavaWEB中读取配置信息
第一种方法是使用java.io和java.util包,缺点是路径的概念要清晰, 例子: Properties prop = new Properties(); InputStream in = get ...