详解iOS学习笔记(十七)——文件操作(NSFileManager)

时间:2022-09-20 09:40:08

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

详解iOS学习笔记(十七)——文件操作(NSFileManager)

上面的完整路径为:用户->资源库->application support->iphone simulator->6.1->aplications

documents:苹果建议将程序创建产生的文件以及应用浏览产生的文件数据保存在该目录下,itunes备份和恢复的时候会包括此目录

library:存储程序的默认设置或其它状态信息;

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

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

详解iOS学习笔记(十七)——文件操作(NSFileManager)

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

获取应用沙盒根路径:

?
1
2
3
4
-(void)dirhome{
  nsstring *dirhome=nshomedirectory();  
  nslog(@"app_home: %@",dirhome);
}

获取documents目录路径:

?
1
2
3
4
5
6
7
8
//获取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目录路径:

?
1
2
3
4
5
6
7
//获取library目录
-(void)dirlib{
  //[nshomedirectory() stringbyappendingpathcomponent:@"library"];
  nsarray *paths = nssearchpathfordirectoriesindomains(nslibrarydirectory, nsuserdomainmask, yes);
  nsstring *librarydirectory = [paths objectatindex:0];
  nslog(@"app_home_lib: %@",librarydirectory);
}

获取cache目录路径:

?
1
2
3
4
5
6
//获取cache目录
-(void)dircache{
  nsarray *cacpath = nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes);
  nsstring *cachepath = [cacpath objectatindex:0];
  nslog(@"app_home_lib_cache: %@",cachepath);
}

获取tmp目录路径:

?
1
2
3
4
5
6
//获取tmp目录
-(void)dirtmp{
  //[nshomedirectory() stringbyappendingpathcomponent:@"tmp"];
  nsstring *tmpdirectory = nstemporarydirectory();
  nslog(@"app_home_tmp: %@",tmpdirectory);
}

创建文件夹:

?
1
2
3
4
5
6
7
8
9
10
11
12
//创建文件夹
-(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(@"文件夹创建失败");
 }

创建文件

?
1
2
3
4
5
6
7
8
9
10
11
12
//创建文件
-(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(@"文件创建失败");
}

写数据到文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
//写文件
-(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(@"文件写入失败");
}

读文件数据:

?
1
2
3
4
5
6
7
8
9
10
//读文件
-(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);
}

文件属性:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//文件属性
-(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);
  }
}

删除文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
//删除文件
-(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");
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/xyz_lmn/article/details/8968213