iOS開發中對檔案目錄的訪問及管理的基本方法小結

2021-07-09 22:13:47 字數 4020 閱讀 4363

檔案目錄的訪問

最簡單的:(由於是沙盒關係,沒有資料夾概念的)

複製**

**如下:

uiimage* image = [uiimage imagenamed:@"11.png"];

還有一種就是需要指定檔案路徑的:

需要用到nsbundle.

複製**

**如下:

[[nsbundle mainbundle] resourcepath],這個就是程式的打包後的路徑。

如果需要指定路徑,就要這樣寫:

你也可自己拼接:

複製**

**如下:

nsstring* path = [nsstringstringwithformat:@"

%@/%@/%@",[[nsbundlemainbundle] resourcepath],@"document",@"aaa.txt"];

或者直接:

複製**

**如下:

nsstring* path = [[nsbundle mainbundle] pathforresource:@"aaa" oftype:@"png"];

一般來說應用程式有3個目錄

documents,library,tmp

一般窩的做法就是儲存在library的caches目錄下面(不知道是否合理:))

tmp目錄的話,儲存一些臨時檔案,在退出程式的時候你可以把裡面的快取內容刪除。

在應用程式中獲得自己的documents目錄:

複製**

**如下:

nsarray * paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);nsstring * documentdirectory = [paths objectatindex:0];

在上面的基礎上,獲得乙個完整的檔案路徑和名字:

複製**

**如下:

這就可以用file來建立,讀取,和寫入檔案。

檔案目錄管理

下面來看一些常用的檔案目錄管理方法

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類進行偏好設定檔案的建立、讀取和修改

控制對檔案的訪問

第二單元 控制對檔案的訪問 截圖檢視17日1 31 檔案系統 是os用於明確儲存裝置 如硬碟 或分割槽上的檔案和資料結構 即在儲存裝置上組織檔案的方法。os中負責管理和儲存檔案資訊的軟體機構稱為檔案管理系統,簡稱檔案系統。檔案系統由三部分組成 檔案系統的介面,對物件操作和管理的軟體集合,物件及屬性。...

控制對檔案的訪問

kiosk foundation0 desktop getfacl file file file 檔名稱 owner kiosk 檔案所有人 group kiosk 檔案所有組 user rw 使用者許可權 user kiosk rwx 特殊指派使用者及許可權 group rw 組許可權 mask ...

控制對檔案的訪問

unit2.2 控制對檔案的訪問 1.許可權列表的讀取 kiosk foundation99 desktop getfacl file file file 檔名稱 owner kiosk 檔案所有人 group kiosk 檔案所有組 user rw 使用者許可權 user kiosk rwx 特殊...