iOS 中獲取各種檔案的目錄路徑的方法

2021-09-02 06:56:18 字數 3913 閱讀 8442

**:

(2)documents目錄:這是文件目錄。有關應用的所有資料檔案應該寫入到這個目錄下,這個目錄用於儲存使用者資料或者其他應該定期備份的資訊。itunes會同步改應用程式的此檔案內容,適合儲存一些重要的資料

(3)libarary目錄:庫目錄,這個目錄下面還有兩個子目錄:caches 和 preferences 

preferences 目錄:包含應用程式的編號設定檔案,存放nsuserdefaults儲存的.plist檔案。itunes同步該應用時會同步該資料夾中的內容。

caches 目錄:快取目錄。用於儲存應用程式專用的支援檔案,儲存應用程式再次啟動過程中需要的資訊。itunes不會同步改資料夾,儲存一些不需要備份的資料           

(4)tmp目錄:臨時目錄。這個目錄用於存放臨時檔案,儲存應用程式再次啟動過程中不再需要的資訊。itunes不會同步此資料夾,系統可能在應用沒執行時就刪除該目錄下的檔案,所以此目錄適合儲存應用中的一些臨時檔案,用完就刪除。

下面就是這些資料夾獲取路徑的方法:

nsstring *path = [[nsbundle mainbundle] bundlepath];

2.獲取documents目錄路徑的方法:

nsstring *documentpath = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes).firstobject;

3.library/caches目錄路徑方法:

nsstring *cachepath = nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes).firstobject;

4.tmp目錄路徑的方法:

nsstring *tmppaht = nstemporarydirectory();

5.獲取沙盒主目錄路徑    

nsstring *homepaht = nshomedirectory();

例子:nsfilemanager* fm=[nsfilemanager defaultmanager];

if(![fm fileexistsatpath:[self datafilepath]])

1、常見的nsfilemanager檔案方法

-(nsdata *)contentsatpath:path  //從乙個檔案讀取資料

-(bool)createfileatpath: path contents:(nsdata *)data attributes:attr  //向乙個檔案寫入資料

-(bool)removeitematpath:path error:err  //刪除乙個檔案

-(bool)moveitematpath:from topath:to error:err  //重新命名或者移動乙個檔案(to不能是已存在的)

-(bool)copyitematpath:from topath:to error:err  //複製檔案(to不能是已存在的)

-(bool)contentsequalatpath:path andpath:path2  //比較兩個檔案的內容

-(bool)fileexistatpath:path  //測試檔案是否存在

-(bool)isreadablefileatpath:path  //測試檔案是否存在,並且是否能執行讀操作  

-(bool)iswriteablefileatpath:path  //測試檔案是否存在,並且是否能執行寫操作  

-(nsdictionary *)attributesofitematpath:path error:err  //獲取檔案的屬性  

-(bool)setattributesofitematpath:attr error:err  //更改檔案的屬性

2.使用目錄

-(nsstring *)currentdirectorypath  //獲取當前目錄

-(bool)changecurrentdirectorypath:path  //更改當前目錄

-(bool)copyitematpath:from topath:to error:err  //複製目錄結構(to不能是已存在的)

-(bool)createdirectoryatpath:path withintermediatedirectories:(bool)flag attribute:attr  //建立乙個新目錄

-(bool)fileexistatpath:path isdirectory:(bool*)flag  //測試檔案是不是目錄(flag中儲存結果yes/no)

-(nsarray *)contentsofdirectoryatpath:path error:err  //列出目錄內容

-(nsdirectoryenumerator *)enumeratoratpath:path  //列舉目錄的內容

-(bool)removeitematpath:path error:err  //刪除空目錄

-(bool)moveitematpath:from topath:to error:err   //重新命名或移動乙個目錄(to不能是已存在的)

3、常用路徑工具方法

+(nsstring *)pathwithcomponens:components  //根據components中的元素構造有效路徑

-(nsarray *)pathcomponents  //析構路徑,獲得組成此路徑的各個部分

-(nsstring *)lastpathcomponent  //提取路徑的最後乙個組成部分

-(nsstring *)pathextension  //從路徑的最後乙個組成部分中提取其副檔名

-(nsstring *)stringbydeletinglastpathcomponent  //刪除路徑的最後乙個組成部分

-(nsstring *)stringbydeletingpathextension  //從檔案的最後一部分刪除副檔名

-(nsstring *)stringbyexpandingtileinpath   //將路徑中代字元擴充套件成使用者主目錄(~)或指定使用者的主目錄(~user)

-(nsstring *)stringbyresolvingsymlinksinpath  //嘗試解析路徑中的符號鏈結

-(nsstring *)stringbystandardizingpath  //通過嘗試解析~、..(父目錄符號)、.(當前目錄符號)和符號鏈結來標準化路徑

4、常用的路徑工具函式

nsstring* nsusername(void)  //返回當前使用者的登入名

nsstring* nsfullusername(void)  //返回當前使用者的完整使用者名稱

nsstring* nshomedirectory(void)  //返回當前使用者主目錄的路徑

nsstring* nshomedirectoryforuser(nsstring* user)  //返回使用者user的主目錄

nsstring* nstemporarydirectory(void)  //返回可用於建立臨時檔案的路徑目錄

5、常用的ios目錄

documents(nsdocumentdirectory)  //用於寫入應用相關資料檔案的目錄,在ios中寫入這裡的檔案能夠與itunes共享並訪問,儲存在這裡的檔案會自動備份到雲端

library/caches(nscachesdirectory)  //用於寫入應用支援檔案的目錄,儲存應用程式再次啟動需要的資訊。itunes不會對這個目錄的內容進行備份

tmp(use nstemporarydirectory())  //這個目錄用於存放臨時檔案,只程式終止時需要移除這些檔案,當應用程式不再需要這些臨時檔案時,應該將其從這個目錄中刪除

library/preferences  //這個目錄包含應用程式的偏好設定檔案,使用 nsuserdefault類進行偏好設定檔案的建立、讀取和修改

IOS中獲取各種檔案的目錄路徑的方法

iphone沙箱模型的有四個資料夾,分別是什麼,永久資料儲存一般放在什麼位置,得到模擬器的路徑的簡單方式是什麼.nshomedirectory 手動儲存的檔案在documents檔案裡 nsuserdefaults儲存的檔案在tmp資料夾裡 1 documents 目錄 您應該將所有 de應用程式資...

IOS中獲取各種檔案的目錄路徑的方法

iphone沙箱模型的有四個資料夾,分別是什麼,永久資料儲存一般放在什麼位置,得到模擬器的路徑的簡單方式是什麼.nshomedirectory 手動儲存的檔案在documents檔案裡 nsuserdefaults儲存的檔案在tmp資料夾裡 1 documents 目錄 您應該將所有 de應用程式資...

IOS中獲取各種檔案的目錄路徑的方法

1.documents 2.library 3.tmp iphone沙箱模型的有四個資料夾,分別是什麼,永久資料儲存一般放在什麼位置,得到模擬器的路徑的簡單方式是什麼.nshomedirectory 手動儲存的檔案在documents檔案裡 nsuserdefaults儲存的檔案在tmp資料夾裡 1...