服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - 详解iOS学习笔记(十七)——文件操作(NSFileManager)

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

2021-02-23 15:10张兴业 IOS

这篇文章主要介绍了详解iOS学习笔记(十七)——文件操作(NSFileManager),具有一定的参考价值,有需要的可以了解一下。

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

延伸 · 阅读

精彩推荐
  • IOSiOS常见的几个修饰词深入讲解

    iOS常见的几个修饰词深入讲解

    这篇文章主要给大家介绍了关于iOS常见的几个修饰词的相关资料,iOS修饰词包括assign、weak、strong、retain、copy、nonatomic、atomic、readonly、readwrite,文中通过示...

    郡王丶千夜7422021-05-10
  • IOSiOS10 Xcode8适配7个常见问题汇总

    iOS10 Xcode8适配7个常见问题汇总

    这篇文章主要为大家详细汇总了iOS10 Xcode8适配7个常见问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    索马里猫10332021-02-01
  • IOSiOS APP实现微信H5支付示例总结

    iOS APP实现微信H5支付示例总结

    这篇文章主要介绍了iOS APP实现微信H5支付示例总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下...

    一张小A11332021-06-01
  • IOS谈一谈iOS单例模式

    谈一谈iOS单例模式

    这篇文章主要和大家谈一谈iOS中的单例模式,单例模式是一种常用的软件设计模式,想要深入了解iOS单例模式的朋友可以参考一下...

    彭盛凇11872021-01-19
  • IOSIOS网络请求之AFNetWorking 3.x 使用详情

    IOS网络请求之AFNetWorking 3.x 使用详情

    本篇文章主要介绍了IOS网络请求之AFNetWorking 3.x 使用详情,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    总李写代码6892021-03-04
  • IOSxcode8提交ipa失败无法构建版本问题的解决方案

    xcode8提交ipa失败无法构建版本问题的解决方案

    xcode升级到xcode8后发现构建不了新的版本。怎么解决呢?下面小编给大家带来了xcode8提交ipa失败无法构建版本问题的解决方案,非常不错,一起看看吧...

    Cinna丶7542021-02-03
  • IOSiOS逆向教程之logify跟踪方法的调用

    iOS逆向教程之logify跟踪方法的调用

    这篇文章主要给大家介绍了关于iOS逆向教程之logify跟踪方法调用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学...

    Mr.Guo11472021-04-28
  • IOSiOS中时间与时间戳的相互转化实例代码

    iOS中时间与时间戳的相互转化实例代码

    这篇文章主要介绍了iOS中时间与时间戳的相互转化实例代码,非常具有实用价值,需要的朋友可以参考下。...

    张无忌!4812021-03-09