iOS的沙盒机制,应用只能访问自己应用目录下的文件。iOS不像android,没有SD卡概念,不能直接访问图像、视频等内容。iOS应用产生的内容,如图像、文件、缓存内容等都必须存储在自己的沙盒内。默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp。Library包含Caches、Preferences目录。

             

上面的完整路径为:用户->资源库->Application Support->iPhone Simulator->6.1->Aplications

Documents:苹果建议将程序创建产生的文件以及应用浏览产生的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录
Library:存储程序的默认设置或其它状态信息;

Library/Caches:存放缓存文件,保存应用的持久化数据,用于应用升级或者应用关闭后的数据保存,不会被itunes同步,所以为了减少同步的时间,可以考虑将一些比较大的文件而又不需要备份的文件放到这个目录下。

tmp:提供一个即时创建临时文件的地方,但不需要持久化,在应用关闭后,该目录下的数据将删除,也可能系统在程序不运行的时候清除。

APP  Sandbox

iOS怎么获取沙盒路径,怎么操作文件呢?下面给出答案。

获取应用沙盒根路径:

-(void)dirHome{
NSString *dirHome=NSHomeDirectory();
NSLog(@"app_home: %@",dirHome);
}

获取Documents目录路径:

//获取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目录路径:

//获取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);
}

创建文件夹:

//创建文件夹
-(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");
}
/**
* @author 张兴业
*  iOS入门群:83702688
*  android开发进阶群:241395671
*  我的新浪微博:@张兴业TBOW
*  我的邮箱:xy-zhang#163.com(#->@)
*/
 
https://developer.apple.com/library/ios/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW2

iOS学习笔记(十七)——文件操作(NSFileManager)的更多相关文章

  1. IOS学习笔记25—HTTP操作之ASIHTTPRequest

    IOS学习笔记25—HTTP操作之ASIHTTPRequest 分类: iOS2012-08-12 10:04 7734人阅读 评论(3) 收藏 举报 iosios5网络wrapper框架新浪微博 A ...

  2. node 学习笔记 - fs 文件操作

    本文同步自我的个人博客:http://www.52cik.com/2015/12/03/learn-node-fs.html 最近看到群里不少大神都开始玩 node 了,我感觉跟他们步伐越来越大了, ...

  3. node学习笔记3——文件操作fs

    文件操作关键字: http('fs') ——  请求 node 里面的 http 模块 readFile ——  读文件,参数包括 文件名,回调函数 writeFile ——  写文件,参数包括 文件 ...

  4. python学习笔记(三):文件操作和集合

    对文件的操作分三步: 1.打开文件获取文件的句柄,句柄就理解为这个文件 2.通过文件句柄操作文件 3.关闭文件. 文件基本操作: f = open('file.txt','r') #以只读方式打开一个 ...

  5. C++ 学习笔记之——文件操作和文件流

    1. 文件的概念 对于用户来说,常用到的文件有两大类:程序文件和数据文件.而根据文件中数据的组织方式,则可以将文件分为 ASCII 文件和二进制文件. ASCII 文件,又称字符文件或者文本文件,它的 ...

  6. Java 学习笔记(14)—— 文件操作

    java文件操作主要封装在Java.io.File中,而文件读写一般采用的是流的方式,Java流封装在 java.io 包中.Java中流可以理解为一个有序的字符序列,从一端导向到另一端.建立了一个流 ...

  7. python学习笔记之文件操作(三)

    这篇博客小波主要介绍一下python对文件的操作 对文件的操作主要分为三步: 1.打开文件获取文件的句柄,句柄也是文件描述符 2.通过文件句柄操作文件 3.关闭文件. 现有以下文件,是小波随写的周杰伦 ...

  8. python学习笔记4(文件操作)

    文件操作: 1.f=open(”caidan”,”w”,encoding=”utf8”)      直接打开一个文件,如果文件不存在则创建文件 f.close() 2.with open (”caid ...

  9. python学习笔记三 文件操作(基础篇)

    文件操作 打开文件 open(name[,mode[,buffering]])   open函数使用一个文件名作为强制参数,然后返回一个文件对象.[python 3.5 把file()删除掉]   w ...

  10. 【java学习笔记】文件操作

    文件操作 java.io.File ①创建删除文件及目录 ②查看文件及目录属性 ③文件过滤器 (PS:不包括文件读写数据) 1.单个文件 创建单个文件,查看属性,删除单个文件. package tmp ...

随机推荐

  1. qml自学笔记------自己写相似于劲舞团的按键小游戏(中)

    接上篇<qml自学笔记------自己写类似于劲舞团的按键小游戏(上)> 第三部分DisplayPart.qml 代码的其它部分都是渣,就这里花了点时间,整个小游戏就靠这个文件. 首先,屏 ...

  2. Python学习笔记(七)函数的使用

    python中的函数使用较简单,这里列出值得注意的几点:   内嵌函数   例如: # coding: utf-8 def foo(): def bar(): print 'bar() called. ...

  3. 在 XenServer上调试windows程序

    WinDbg WinDbg is one of a number of tools available from Microsoft that can be used for debugging Wi ...

  4. linux c中select使用技巧——计时器(转)

    通过本文你会了解到: 1. select()原型及参数说明 2. select()应用情景 3. select()注意事项 4. select()作定时器 原型 int select(int nfds ...

  5. android 类似微信的摇一摇实现

    一.在 AndroidManifest.xml 中添加操作权限 <uses-permission android:name="android.permission.VIBRATE&qu ...

  6. Jmeter变量参数化及函数应用

    分类: 测试工具 2006-12-14 10:54 12041人阅读 评论(5) 收藏 举报 javascriptloadrunnerrandom脚本测试多线程 我们在使用Jmeter录制脚本后,经常 ...

  7. 压测过程中,CPU和内存占用率很高,案例简单分析

    Q:  最近公司测试一个接口,数据库采用Mongo    并发策略:并发400个用户,每3秒加载5个用户,持续运行30分钟    数据量:8000条左右 压测结果发现:    TPS始终在5左右   ...

  8. 在php中修改cookie值遇到的奇怪问题

    本想修改cookie的值比较简单,结果测试发现并不是. 刚开始实现cookie修改的思路:先删除以前的cookie值,再创建一个新的. setcookie('name',value,time()-1) ...

  9. Android解决button反复点击问题

    public class BaseActivity extends Activity { protected boolean isDestroy; //防止反复点击设置的标志.涉及到点击打开其它Act ...

  10. Atitit.  c# 语法新特性 c#2.0 3.0 4.0 4.5 5.0 6.0   attilax总结

    Atitit.  c# 语法新特性 c#2.0 3.0 4.0 4.5 5.0 6.0   attilax总结 1.1. C# 1.0-纯粹的面向对象 1.2. C# 2.0-泛型编程新概念 1.3. ...