iOS筆記 檔案的寫入 讀取與操作

2021-08-20 06:45:07 字數 3832 閱讀 8549

有時候,專案中需要將資料持久化儲存。我們的做法之一就是將其寫入檔案。當需要的時候可以從檔案中讀取出來即可。

//

// main.m

// 寫入檔案

//// created by hhg on 15/5/28.

//#import

int main(int argc, const

char * argv) ;

nsdata *data =[[nsdata alloc]initwithcontentsoffile:stringpath];

bool iswritedata =[data writetofile:datapath atomically:yes];

if (iswritedata)

// 陣列在寫檔案時 包含元素只能是 nsstring nsarray nsdata nsnumber nsdictionary

nsstring *sannamestr = @"張三";

nsstring *sinamestr = @"李四";

nsstring *wunamestr = @"王五";

nsarray *namearray = [nsarray arraywithobjects:sannamestr, sinamestr, wunamestr,nil];

bool iswritearray =[namearray writetofile:arraypath atomically:yes];

if (iswritearray)

/// 讀取檔案

nsstring *readstr =[[nsstring alloc]initwithdata:data encoding:nsutf8stringencoding];

nslog(@" 讀取的string為 : %@", readstr);

nsstring *readdatastr =[[nsstring alloc]initwithdata:data encoding:nsutf8stringencoding];

nslog(@" 讀取的data為 :%@", readdatastr);

nsarray *readarr =[[nsarray alloc]initwithcontentsoffile:arraypath];

nslog(@"讀取的array為 :%@",readarr);

//字典存入plist檔案

nsdictionary *dic =[[nsdictionary alloc]initwithobjectsandkeys:

sannamestr,@"第一",

sinamestr,@"第二",

wunamestr,@"第三",

namearray,@"姓名陣列", nil];

[dic writetofile:arraypath atomically:yes];

nsdictionary *mydic =[[nsdictionary alloc]initwithcontentsoffile:arraypath];

nsarray * namearr =mydic[@"姓名陣列"];

nslog(@"namearr : %@",namearr);

}return

0;}

當我們正常寫完資料之後,後面需要新增新的資料並不能直接像上面那樣再次操作。一旦再次操作,會覆蓋掉原先的資料。為了避免覆蓋,我們需要對新增的資料進行偏移操作。

//

// main.m

// 新增資料

//// created by hhg on 15/4/29.

//#import

int main(int argc, const

char * argv)

//獲取檔案的總長度

nsfilemanager *filemanager =[nsfilemanager defaultmanager];

nsdictionary *dic =[filemanager attributesofitematpath:path error:nil];

nslog(@"dic : %@",dic);

nsnumber *number =[dic objectforkey:nsfilesize];

int length = [number intvalue];

nslog(@"length = %d", length);

//設定檔案讀取的偏移量

[readfilehandle seektofileoffset:length/2];

nsdata *readdata =[readfilehandle readdatatoendoffile];//讀到結尾

//從開始讀取檔案一直讀取到某一位置

// - (nsdata *)readdataoflength:(nsuinteger)length;

nsstring*readstr =[[nsstring alloc]initwithdata:readdata encoding:nsutf8stringencoding];

nslog(@"readstr : %@",readstr);

[readfilehandle closefile];

}return

0;}

當儲存完資料後,因業務需求,需要改變資料儲存的位置,我們就需要對檔案進行移動或刪除等操作。

//

// main.m

// nsfilemanager

//// created by hhg on 15/5/28.

//#import

int main(int argc, const

char * argv)

/*___________讀取檔案__________*/

//讀取二進位制資料

nsdata *readdata =[file contentsatpath:path];

//將二進位制資料轉碼

nsstring *readstr =[[nsstring alloc]initwithdata:readdata encoding:nsutf8stringencoding];

nslog(@"讀取檔案的內容: %@",readstr);

/* __________檔案的移動複製剪下______________ */

//移動

nsstring *movepath = @"/users/hhg/desktop/myfolder/file.txt";

bool ismove = [file moveitematpath:path topath:movepath error:nil];

if (ismove)

//檔案複製

[file copyitematpath:movepath topath:folderpath error:nil];

/* _________檔案的刪除____________*/

bool isdelete = [file fileexistsatpath:movepath];

if ( isdelete )

nsdictionary *dic = [file attributesofitematpath:movepath error:nil];

nsnumber *num = [dic objectforkey:nsfilesize];

nslog(@"字典:%@", dic);

nslog(@" 尺寸大小:%@", num);

//-->建立資料夾->建立檔案(寫入字串)-> 移動檔案 ->讀取檔案內容->刪除檔案

}return

0;}

iOS檔案寫入讀取

最近在學習 ios開發中的資料持久化裡的檔案讀取 以下是自己的寫的一點東西 還請各位看官老爺多多指點 ios在進行資料持久化中採用沙盒,不允許非本應用程式以外的其他應用程式訪問該應用系統的一些檔案.ios8以後沙盒許可權不再像以前那麼嚴格 在進行本地檔案讀取操作中,首先需要明確 路徑 這個 詞 我們...

PHP檔案操作 讀取與寫入

php檔案系統是基於unix系統的 檔案資料基本型別 二進位制資料 文字資料 檔案輸入流 資料從原始檔到記憶體的流動 檔案輸出流 資料從記憶體儲存到檔案的流動 檔案操作函式 獲取檔案流 fopen 檔案相對路徑 絕對路徑,檔案開啟模式 成功返回檔案流,否則返回false 檔案開啟模式 六種常用開啟方...

檔案操作 檔案的讀取與寫入

stream open file,mode stream流 通過流進行讀或者寫 read 讀的方法有 read 讀取所有內容 readline 每次讀取一行內容 readlines 讀取所有的行儲存到列表中 readable 判斷是否可讀的 write 寫內容 寫的方法有 write 內容 然後寫當...