簡單物件寫入檔案

2022-10-10 09:42:15 字數 2634 閱讀 1982

//簡單物件寫入檔案

//僅支援nsstring,nsarray,nsdictionary,nsdata

//獲取document路徑

// nsstring

nsstring * documentpath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) lastobject];

nsstring * str = @"i love my ios teacher!";

[str writetofile:path atomically:yes encoding:nsutf8stringencoding error:nil];

nslog(@"%@",path);

//讀取檔案

nsstring * content = [nsstring stringwithcontentsoffile:path encoding:nsutf8stringencoding error:nil];

nslog(@"%@",content);

//nsarray

nsarray * array = @[@"i",@"love",@"my",@"ios",@"teacher"];

[array writetofile:path atomically:yes];

//讀取檔案

nsarray * array2 = [nsarray arraywithcontentsoffile:path];

nslog(@"%@",array2);

nsarray * array3 = [[nsarray alloc] initwithcontentsoffile:path];

nslog(@"%@",array3);

//字典

nsstring * cachepath = [nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes) lastobject];

nsdictionary * dic = @;

[dic writetofile:path2 atomically:yes];

nsdictionary * dic2 = [nsdictionary dictionarywithcontentsoffile:path2];

nslog(@"%@",dic2);

//nsdata

uiimage * image = [uiimage imagenamed:@"1"];

//    uiimageview * im**iew = [[uiimageview alloc] initwithimage:image];

//    [self.view addsubview:im**iew];

//    nslog(@"%@",im**iew);

nsstring * temppath = nstemporarydirectory();

//將轉化成資料物件

nsdata * data = uiimagepngrepresentation(image);

//寫入檔案

[data writetofile:temppath atomically:yes];

nslog(@"%@",temppath);

//讀取檔案

nsdata * _data = [nsdata datawithcontentsoffile:temppath];

uiimage * _image = [uiimage imagewithdata:_data];

nslog(@"%@",_image);

//    nsuserdefaults * usr = [nsuserdefaults standarduserdefaults];

//    [usr setobject:@"tao" forkey:@"master"];

//    [self test];

//nsfilemanager

nsfilemanager * file = [[nsfilemanager alloc] init];

nsfilemanager * file1 = [nsfilemanager defaultmanager];

nsstring * homepath = nshomedirectory();

[file createdirectoryatpath:homepath withintermediatedirectories:yes attributes:nil error:nil];

//新建資料夾

[file createdirectoryatpath:homepath2 withintermediatedirectories:yes attributes:nil error:nil];

//刪除資料夾

[file removeitematpath:homepath2 error:nil];

//拷貝資料夾

//    [file copyitematpath:homepath topath:homepath2 error:nil];//(死迴圈了)

//移動

[file moveitematpath:homepath2 topath:homepath error:nil];

IOS之資料持久化 簡單物件寫入檔案

寫入和讀取 寫入 ibaction wirtebt id sender 檔案拼接 寫入 dic writetofile firlpath atomically yes pragma mark data寫入本地 data nsdata data str1 datausingencoding nsutf...

檔案位元組流讀取檔案 寫入檔案簡單demo

sneakythrows public static void main string args 建立檔案位元組輸入流 引數 要讀取檔案指定的位址 fileinputstream fileinputstream new fileinputstream file 建立檔案位元組輸出流 引數 輸出檔案指...

日誌寫入到檔案 多檔案寫入

在上篇文章的基礎上,修改配置 log4j.rootlogger info,fout 說明 rootlogger是可以多樣式定義的,如log4j.rootlogger info,myout,fout。myout,fout是自定義樣式。這樣就會在c盤下建立a.log檔案。有這樣一種需求,不同模組需要各自...