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

 
 
 

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. }

iOS——文件操作NSFileManager (创建、删除,复制,粘贴)的更多相关文章

  1. php -- 文件操作(创建、复制、移动、删除)

    创建 文件夹 bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $con ...

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

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

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

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

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

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

  5. iOS - 文件操作(File Operating)

    1. 沙盒 & NSData /*_______________________________获取沙盒路径_________________________________________* ...

  6. ios 文件操作

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

  7. 文件操作 - NSFileManager

    iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视频等内容.iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙盒内.默认 ...

  8. 【精】iOS 文件操作:沙盒(SandBox)、文件操作(FileManager)、程序包(NSBundle)

    1.沙盒机制介绍 iOS 中的沙盒机制(SandBox)是一种安全体系. 每一个 iOS 应用程序都有一个单独的文件系统(存储空间).并且仅仅能在相应的文件系统中进行操作,此区域被称为沙盒. 全部的非 ...

  9. Windows、Linux下文件操作(写、删除)错误的产生原因、及解决方法

    catalog . 引言 . Linux平台上涉及的File IO操作 . Windows平台上涉及的File IO操作 0. 引言 本文试图讨论在windows.linux操作系统上基于C库进行文件 ...

随机推荐

  1. delphi 通过控件的handle取得控件

    例子代码如下: vartsg:TstringGrid;begintsg:=Tstringgrid(FindControl(handle));//正常使用TstringGrid//tsg......./ ...

  2. 【树形动态规划】【CTSC1997】选课 解题报告

    CTSC1997-选课 描述 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校开设了N(N<300)门的选修课程,每个学生可选课程的数量M是给定的.学生选修了这 ...

  3. OBJ-C

    1.直接赋值 NSString *name = @"Starain"; 2.用已经存在的字符串进行初始化 NSString *name2 = [NSString stringWit ...

  4. FLEX 图片拷贝

    在用FLEX做GIS相关的开发的时候,遇到一个问题.因为是监控类的系统,所以需要要求地图上的ICON的实时更新,从而会出现重复加载的情况.就是重复请求相同的图片用做背景,尤其是在加载的ICON较多的时 ...

  5. VC++中操作XMLWin32实例

    摘要:VC++中操作XML XML在Win32程序方面应该没有在Web方面应用得多,很多Win32程序也只是用XML来存存配置信息而已,而且没有足够的好处的话还不如用ini.VC++里操作XML有两个 ...

  6. BlockingQueue接口

    BlockingQueue接口定义了一种阻塞的FIFO queue,每一个BlockingQueue都有一个容量,让容量满时往BlockingQueue中添加数据时会阻塞,当容量为空时取元素操作会阻塞 ...

  7. .net错误处理机制(转)

    asp.net 提供了4中错误机制:Page_Error事件>ErrorPage属性>Application_Error事件> <customErrors>配置项 ① P ...

  8. 遍历页面上所有TextBox,并赋值为String.Empty

    //不含母板页 foreach (System.Web.UI.Control txtobj in this.Page.Controls)   {     if (txtobj.GetType().Na ...

  9. 如何分析apache日志[access_log(访问日志)和error_log(错误日志)]

    如何分析apache日志[access_log(访问日志)和error_log(错误日志)] 发布时间: 2013-12-17 浏览次数:205 分类: 服务器 默认Apache运行会access_l ...

  10. [转]MySQL数据库的热备份

    一.系统环境描述:      1.两台数据库服务器,A和B:      2.当前A正在使用,将作为主服务器,B为准备用来做备用数据库服务器:      3.要进行热备份的数据库中含有类型为MyISAM ...