当前位置: 移动技术网 > IT编程>移动开发>IOS > 笔记:沙盒文件的创建(我懒,偷偷复制过来的,无奈脸。。。。)

笔记:沙盒文件的创建(我懒,偷偷复制过来的,无奈脸。。。。)

2019年01月23日  | 移动技术网IT编程  | 我要评论

虎骨,扭力检测仪,恩阳中学

    首先,先总结一下如何获取documents目录,在ios开发中,我们经常需要检索documents目录的完整路径以便读取和写入文件,我总结了以下两种方法:
1、nsstring *documentsdirectory = [nshomedirectory() stringbyappendingpathcomponent:@"documents"];
2、nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);
   nsstring *documentsdirectory = [paths objectatindex:0];
 以上documentsdirectory就是获取的documents的完整路径。
 程序的所有资源文件,存储在程序包中,获取程序包路径的方法是:
nsstring *appdirectory = [[nsbundle mainbundle] bundlepath];
在实际开发中我们有时需要判断documents下的某个资源文件是否存在,如果不存在,则从程序包中拷贝进去,现在假设要判定的文件是"xxx.txt",判定代码如下:
nsfilemanager *filemanager = [nsfilemanager defaultmanager];
//nsstring *filepath = [self [documentsdirectory stringbyappendingpathcomponent:@"xxx.txt"]];

nsstring *filepath = [documentsdirectory stringbyappendingpathcomponent:@"xxx.txt"];

if(![filemanager fileexistsatpath:filepath]) //如果不存在
{
     nslog(@"xxx.txt is not exist");
     nsstring *datapath = [[[nsbundle mainbundle] bundlepath] stringbyappendingstring:@"/xxx.txt"];//获取程序包中相应文件的路径
     nserror *error;
     if([filemanager copyitematpath:datapath topath:filepath error:&error]) //拷贝
     {
          nslog(@"copy xxx.txt success");
     }
     else 
     {
          nslog(@"%@",error);
     }  
}

 https://www.cnblogs.com/fbiosblog/p/5819418.html

https://blog.csdn.net/csdn_hhg/article/details/80458646

https://www.jianshu.com/p/2d0ad82c59fa//读写

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网