天天看点

沙盒操作文件——文件操作(NSFileManager)

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

沙盒操作文件——文件操作(NSFileManager)
沙盒操作文件——文件操作(NSFileManager)

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

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

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

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

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

沙盒操作文件——文件操作(NSFileManager)

app  sandbox

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

-(void)dirhome{  

    nsstring *dirhome=nshomedirectory();      

    nslog(@"app_home: %@",dirhome);  

}  

//获取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目录  

-(void)dirlib{  

    //[nshomedirectory() stringbyappendingpathcomponent:@"library"];  

    nsarray *paths = nssearchpathfordirectoriesindomains(nslibrarydirectory, nsuserdomainmask, yes);  

    nsstring *librarydirectory = [paths objectatindex:0];  

    nslog(@"app_home_lib: %@",librarydirectory);  

//获取cache目录  

-(void)dircache{  

    nsarray *cacpath = nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes);  

    nsstring *cachepath = [cacpath objectatindex:0];  

    nslog(@"app_home_lib_cache: %@",cachepath);  

//获取tmp目录  

-(void)dirtmp{  

    //[nshomedirectory() stringbyappendingpathcomponent:@"tmp"];  

    nsstring *tmpdirectory = nstemporarydirectory();  

    nslog(@"app_home_tmp: %@",tmpdirectory);  

//创建文件夹  

-(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(@"文件夹创建失败");  

 }  

//创建文件  

-(void *)createfile{  

    nsstring *testpath = [testdirectory stringbyappendingpathcomponent:@"test.txt"];  

    bool res=[filemanager createfileatpath:testpath contents:nil attributes:nil];  

        nslog(@"文件创建成功: %@" ,testpath);  

        nslog(@"文件创建失败");  

//写文件  

-(void)writefile{  

    nsstring *content=@"测试写入内容!";  

    bool res=[content writetofile:testpath atomically:yes encoding:nsutf8stringencoding error:nil];  

        nslog(@"文件写入成功");  

        nslog(@"文件写入失败");  

//读文件  

-(void)readfile{  

//    nsdata *data = [nsdata datawithcontentsoffile:testpath];  

//    nslog(@"文件读取成功: %@",[[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding]);  

    nsstring *content=[nsstring stringwithcontentsoffile:testpath encoding:nsutf8stringencoding error:nil];  

    nslog(@"文件读取成功: %@",content);  

//文件属性  

-(void)fileattriutes{  

    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);  

    }  

//删除文件  

-(void)deletefile{  

    nsstring *testpath = [testdirectory stringbyappendingpathcomponent:@"test.txt"];     

    bool res=[filemanager removeitematpath:testpath error:nil];  

        nslog(@"文件删除成功");  

        nslog(@"文件删除失败");     

    nslog(@"文件是否存在: %@",[filemanager isexecutablefileatpath:testpath]?@"yes":@"no");  

}  

继续阅读