天天看點

沙盒操作檔案——檔案操作(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");  

}  

繼續閱讀