在這個目錄下面,有四個目錄需要了解:
-documents -- 這是用來儲存使用者檔案的首選目錄。
-library -- 這個目錄作為preference目錄的父目錄而單獨存在
-tmp --
訪問檔案:
nsfilemanager 是用來訪問檔案系統的主要類:
nsfilemanager*filemanager = [nsfilemanager
defaultmanager];
<>查詢檔案:
nsarray
*directorypaths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask,
yes);
// 傳遞 0 代表是找在documents 目錄下的檔案。
nsstring *documentdirectory = [paths objectatindex:0];
// dbname 是要查詢的檔案名字,檔案全名
// 用這個方法來判斷當前的檔案是否存在,如果不存在,就建立乙個檔案
if ( ![filemanager fileexistsatpath:path])
<3>讀取檔案資料:
//分別用nsdata 和nsstring,nsmutabledictionary來讀取檔案內容
nsdata* filedata = [nsdata datawithcontentsoffile:dbname];
nsstring* mystring = [nsstring stringwithcontentsoffile:dbname usedencoding:null error:null];
nsmutabledictionary* dict = [[nsmutabledictionary alloc]initwithcontentsoffile:filename];
<4>把資料寫入檔案
[filedata writetofile:filename atomically:yes]
ios 檔案讀寫
分別是 既然要進行檔案讀寫,那我們就需要得到檔案目錄路徑,得到路徑的方法有以下幾種 1 先獲取根目錄,然後進行字串拼接 例如 獲取程式根目錄 nsstring homepath nshomedirectory 獲取根目錄下的documents目錄 或者。這個與上面方法的區別在於要不要加 2 一般對於...
iOS檔案讀寫
void viewdidload 從指定的路徑下讀取檔案的資料 原則 寫入什麼型別,需要用該型別接收 nsarray arrayfromfile nsarray arraywithcontentsoffile filepath 需求二 把字典資料寫入檔案中 nsdictionary dic dic ...
ios中讀寫檔案
在ios的開發中,經常遇到要讀寫檔案的情況,例如處理 採集資訊等,那麼在ios中對檔案的讀寫有兩種方式 1 使用nsdata 來將整個資料讀取到記憶體中 將檔案寫到檔案中 使用這種方式比較適合針對小檔案的讀寫,可以全部的讀到記憶體中處理,比如說全域性性的配置檔案等。2 使用c的api來讀取 在ios...