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怎么获取沙盒路径,怎么操作文件呢?下面给出答案。

获取应用沙盒根路径:

  1. -(void)dirHome{
  2. NSString *dirHome=NSHomeDirectory();
  3. NSLog(@"app_home: %@",dirHome);
  4. }

获取Documents目录路径:

  1. //获取Documents目录
  2. -(NSString *)dirDoc{
  3. //[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  4. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  5. NSString *documentsDirectory = [paths objectAtIndex:0];
  6. NSLog(@"app_home_doc: %@",documentsDirectory);
  7. return documentsDirectory;
  8. }

获取Library目录路径:

  1. //获取Library目录
  2. -(void)dirLib{
  3. //[NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
  4. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
  5. NSString *libraryDirectory = [paths objectAtIndex:0];
  6. NSLog(@"app_home_lib: %@",libraryDirectory);
  7. }

获取Cache目录路径:

  1. //获取Cache目录
  2. -(void)dirCache{
  3. NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4. NSString *cachePath = [cacPath objectAtIndex:0];
  5. NSLog(@"app_home_lib_cache: %@",cachePath);
  6. }

获取Tmp目录路径:

  1. //获取Tmp目录
  2. -(void)dirTmp{
  3. //[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];
  4. NSString *tmpDirectory = NSTemporaryDirectory();
  5. NSLog(@"app_home_tmp: %@",tmpDirectory);
  6. }

创建文件夹:

  1. //创建文件夹
  2. -(void *)createDir{
  3. NSString *documentsPath =[self dirDoc];
  4. NSFileManager *fileManager = [NSFileManager defaultManager];
  5. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
  6. // 创建目录
  7. BOOL res=[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
  8. if (res) {
  9. NSLog(@"文件夹创建成功");
  10. }else
  11. NSLog(@"文件夹创建失败");
  12. }

创建文件

  1. //创建文件
  2. -(void *)createFile{
  3. NSString *documentsPath =[self dirDoc];
  4. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
  5. NSFileManager *fileManager = [NSFileManager defaultManager];
  6. NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
  7. BOOL res=[fileManager createFileAtPath:testPath contents:nil attributes:nil];
  8. if (res) {
  9. NSLog(@"文件创建成功: %@" ,testPath);
  10. }else
  11. NSLog(@"文件创建失败");
  12. }

写数据到文件:

  1. //写文件
  2. -(void)writeFile{
  3. NSString *documentsPath =[self dirDoc];
  4. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
  5. NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
  6. NSString *content=@"测试写入内容!";
  7. BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  8. if (res) {
  9. NSLog(@"文件写入成功");
  10. }else
  11. NSLog(@"文件写入失败");
  12. }

读文件数据:

  1. //读文件
  2. -(void)readFile{
  3. NSString *documentsPath =[self dirDoc];
  4. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
  5. NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
  6. //    NSData *data = [NSData dataWithContentsOfFile:testPath];
  7. //    NSLog(@"文件读取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
  8. NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil];
  9. NSLog(@"文件读取成功: %@",content);
  10. }

文件属性:

  1. //文件属性
  2. -(void)fileAttriutes{
  3. NSString *documentsPath =[self dirDoc];
  4. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
  5. NSFileManager *fileManager = [NSFileManager defaultManager];
  6. NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
  7. NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:testPath error:nil];
  8. NSArray *keys;
  9. id key, value;
  10. keys = [fileAttributes allKeys];
  11. int count = [keys count];
  12. for (int i = 0; i < count; i++)
  13. {
  14. key = [keys objectAtIndex: i];
  15. value = [fileAttributes objectForKey: key];
  16. NSLog (@"Key: %@ for value: %@", key, value);
  17. }
  18. }

删除文件:

    1. //删除文件
    2. -(void)deleteFile{
    3. NSString *documentsPath =[self dirDoc];
    4. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
    5. NSFileManager *fileManager = [NSFileManager defaultManager];
    6. NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
    7. BOOL res=[fileManager removeItemAtPath:testPath error:nil];
    8. if (res) {
    9. NSLog(@"文件删除成功");
    10. }else
    11. NSLog(@"文件删除失败");
    12. NSLog(@"文件是否存在: %@",[fileManager isExecutableFileAtPath:testPath]?@"YES":@"NO");
    13. }

文件操作 - NSFileManager的更多相关文章

  1. iOS——文件操作NSFileManager (创建、删除,复制,粘贴)

    iOS——文件操作NSFileManager (创建.删除,复制,粘贴)       iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视 ...

  2. IOS 开发文件操作——NSFileManager

    转自:http://blog.csdn.net/xyz_lmn/article/details/8968213,留着方便查阅 iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像androi ...

  3. iOS文件和目录操作,iOS文件操作,NSFileManager使用文件操作:

    NSFileManager常用的文件方法: -(NSData*)contentsAtPath:path 从一个文件中读取数据 -(BOLL)createFileAtPath:path contents ...

  4. iOS 关于文件操作 NSFileManager

    创建文件夹 + (BOOL)creatDir:(NSString *)path { if (path.length==0) { return NO; } NSFileManager *fileMana ...

  5. Objective-C文件和目录操作,IOS文件操作,NSFileManager使用文件操作

    http://blog.csdn.net/swingpyzf/article/details/15185767

  6. 归档NSKeyedArchiver解归档NSKeyedUnarchiver与文件管理类NSFileManager (文件操作)

    ========================== 文件操作 ========================== 一.归档NSKeyedArchiver 1.第一种方式:存储一种数据. // 归档 ...

  7. NSFileManager文件操作的十个小功能

    NSFileManager文件操作的十个小功能 NSFileManager是一个单列类,也是一个文件管理器.可以通过NSFileManager创建文件夹.创建文件.写文件.读文件内容等等基本功能. 下 ...

  8. IOS文件操作的两种方式:NSFileManager操作和流操作

    1.常见的NSFileManager文件方法 -(NSData *)contentsAtPath:path //从一个文件读取数据 -(BOOL)createFileAtPath: path cont ...

  9. Foundation 框架 NSFileManager,NSData 简单的文件操作

    一.简单展示NSFileManager的使用 #import <Foundation/Foundation.h> int main(int argc, const char * argv[ ...

随机推荐

  1. [AngularJS + Webpack] require directives

    direictives/index.js: module.exports = function(ngModule) { //register all the directives here requi ...

  2. Java调用yahoo!API获取天气数据

    先把代码复制上来,以后再做补充 package com.weather.test; import java.io.InputStream; import java.net.URL; import ja ...

  3. merge into sql优化

    今天网友说他的merge into sql跑了15分钟了还没有跑出数据,问我能不能优化一下,我让他把sql和sql的执行计划发过来 merge into F_Sal_P_Camp_Samp_Cust_ ...

  4. Java基础知识强化之IO流笔记52:IO流练习之 把一个文件中的字符串排序后再写入另一个文件案例

    1. 把一个文件中的字符串排序后再写入另一个文件 已知s.txt文件中有这样的一个字符串:"hcexfgijkamdnoqrzstuvwybpl" 请编写程序读取数据内容,把数据排 ...

  5. Java基础知识强化之IO流笔记41:字符流缓冲流之复制文本文件案例02(使用 [ newLine() / readLine() ] )(重要)

    1. 使用字符流缓冲流的特殊功能 [ newLine() / readLine() ] 需求:把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中  数据源: a.txt -- 读取数据 ...

  6. iOS之XIB拖拽scrollView

    在使用storyboard和xib时,我们经常要用到ScrollView,还有自动布局AutoLayout,但是ScrollView和AutoLayout 结合使用,相对来说有点复杂.根据实践,我说一 ...

  7. 深入理解计算机系统第二版习题解答CSAPP 2.6

    使用打印字节的方式可以知道十进制数12345的十六进制为0x00003039,十进制浮点数12345.0的十六进制为0x4640E400. 转换为二进制为 /* 0 0 0 0 3 0 3 9 *** ...

  8. semaphore(信号量)使用说明

    例子:      以一个停车场运作为例.为了简单起见,假设停车场只有三个车位,一开始三个车位都是空的.这时如果同时来了五辆车,看门人允许其中三辆不受阻碍的进入,然后放下车拦,剩下的车则必须在入口等待, ...

  9. CentOs6.8安装Git并安装oh my zsh

    (一)git安装 1.下载git2.4.9或其他版本 Index of /pub/software/scm/git git各个版本下载链接: https://www.kernel.org/pub/so ...

  10. Atom编辑器入门到精通(五) Git支持

    版本控制对于开发来说非常重要,Atom当然也提供了很好的支持,本文将介绍如何在Atom中集成使用Git和GitHub 恢复文件 当你修改了某个文件,然后发现改得不满意,希望恢复文件到最后一次提交的状态 ...