IOS管理檔案和目錄NSFileManager

2022-08-17 22:12:16 字數 2777 閱讀 9770

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管理檔案和目錄

1 常見的nsfilemanager檔案方法 nsdata contentsatpath path 從乙個檔案讀取資料 bool createfileatpath path contents nsdata data attributes attr 向乙個檔案寫入資料 bool removeitema...

IOS管理檔案和目錄

1 常見的nsfilemanager檔案方法 nsdata contentsatpath path 從乙個檔案讀取資料 bool createfileatpath path contents nsdata data attributes attr 向乙個檔案寫入資料 bool removeitema...

檔案和目錄管理

mkdir 建立目錄 mkdir p 級聯建立目錄 rm 刪除檔案和目錄 rm r 刪除目錄用的選項 rm rf 強制刪除不提示 慎用 cp 複製檔案或目錄 copy的簡寫 cp option 檔案 目地檔案 cp r 複製目錄需要加 r mv 移動檔案或目錄 move的簡寫 mv 檔案 目地檔案 ...