笔记:沙盒文件的创建(我懒,偷偷复制过来的,无奈脸。。。。)
程序员文章站
2022-11-21 20:55:01
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"xxx.txt"]; https://www.cnblogs.com/FBiOSBlog/p/5819418.html https://blog.csd ......
首先,先总结一下如何获取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//读写