下面是一些簡單的關於oc中對檔案的基本操作的案例:
一些必要的說明//應用程式檔案的移動
[[nsfilemanager new] moveitematpath:(nsstring *)/*路徑格式(/user/……)-nsstring型別*/ topath:(nsstring *) error:(nserror *__autoreleasing *)];
//應用程式檔案的複製
[[nsfilemanager new] copyitematpath:(nsstring *) topath:(nsstring *) error:(nserror *__autoreleasing *)];
//應用程式檔案的刪除
[[nsfilemanager new] removeitematpath:(nsstring *) error:(nserror *__autoreleasing *)];
//應用程式建立資料夾
[[nsfilemanager new] createdirectoryatpath:(nsstring *) withintermediatedirectories:(bool) attributes:(nsdictionary *) error:(nserror *__autoreleasing *)];
//應用程式建立檔案
[[nsfilemanager new] createfileatpath:(nsstring *) contents:(nsdata *) attributes:(nsdictionary *)];
應用程式檔案的寫入#define fm [nsfilemanager new]
#define documentspath [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]
//巨集定義減少**書寫量
typedef enum downloadtype;
//列舉能夠返回各種情況,比bool強大。
typedef enum removetype;
應用程式資料夾內容的刪除(nsstring *)urlstring callfilename:(nsstring *)filename
//為了保證健壯性,進行轉碼,防止中文和特殊符號
urlstring = [urlstring stringbyaddingpercentescapesusingencoding:4];
nsdata *data = [nsdata datawithcontentsofurl:[nsurl urlwithstring:urlstring]];
if (data == nil)
if (filename ==nil)
//路徑
//開始寫入
if (![fm fileexistsatpath:filepath]) else
//判斷寫入是否成功
if (![fm fileexistsatpath:filepath])
return
type;
}
刪除指定檔案+(removetype) removeallimageindirectoryname:(nsstring *)directoryname
//找到資料夾中的檔案,返回陣列
nsarray *array = [fm subpathsatpath:directoryname];
//遍歷陣列時刪除資料夾中檔案
for (nsstring *obj in array)
//判斷是否刪除成功
if ([fm subpathsatpath:directoryname].count!=0)
return success;
}
求應用檔案的大小+(removetype) removefilename:(nsstring *)filename indirectoryname:(nsstring *)directoryname else
if ([fm fileexistsatpath:filename])
return success;
}
+(nsstring *) caculatesizewithdirectoryname:(nsstring *)directoryname
size = [nsstring stringwithformat:@"%.4lfm",count/1024/1024];
return size;
}
oc檔案操作
documents 蘋果建議將程式建立產生的檔案以及應用瀏覽產生的檔案資料儲存在該目錄下,itunes備份和恢復的時候會包括此目錄 library 儲存程式的預設設定或其它狀態資訊 library caches 存放快取檔案,儲存應用的持久化資料,用於應用公升級或者應用關閉後的資料儲存,不會被itu...
OC學習 檔案操作
oc中檔案的操作可以分為兩類 1 檔案本省的操作 建立 刪除 移動 拷貝等 2 檔案內容的操作 讀 寫等 讀 磁碟 記憶體 寫 記憶體 磁碟 要想學會oc中的檔案操作,我們就要學會兩個類 nsfilemanager 檔案管理類 nsfilehandle 檔案控制代碼類 一 nsfilemanager...
OC學習筆記 OC中的類
1.objc跟c的區別與聯絡 首先 objc是乙個物件導向的語言 封裝 繼承 多型 objc相當於c的乙個超集 即objc允許使用任何c語言 但增加了許多c沒有的特點 bigger easier 2.objc中的類與物件 類由介面 xx.h inte ce 和實現 xx.m implementati...