2013 07 19 檔案操作

2021-06-17 00:26:26 字數 2696 閱讀 4269

**:

//init方法、便利構造器  這是oc中初始化物件的兩種方法

nsstring *path1= @"/users/1010/desktop/abc.txt";

//用乙個檔案來初始化乙個字串,也就是讀檔案中的內容

nserror *error = nil;

// nsstring *content1 = [[nsstring alloc]initwithcontentsoffile:path1 encoding:nsutf8stringencoding error:&error];

nsstring *content1 = [nsstring

stringwithcontentsoffile:path1 encoding:4

error:&error];

if (error != nil)

nslog(@"content1 = %@",content1);

//寫檔案,它會覆蓋掉檔案中以前的內容

nsstring

*str1 = @"i am learning ios";

bool s =  [str1 writetofile:path1

atomically:yes

encoding:4

error

:&error];//atomically 若為no,則str1會立刻寫入到檔案中,若為yes,則先寫入到快取中,當該檔案為空閒的時候,在從快取寫入到檔案中

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

//   nsdata *data1 = [[nsdata alloc]initwithcontentsoffile:path1];//init初始化

nsdata *data1 = [nsdata

datawithcontentsoffile:path1];//

便利構造器初始化

nslog(@"data1 = %@",data1);

nsstring *str2 = [[nsstring

alloc]initwithdata:data1 encoding:4];//encoding:4,4代表utf-8編碼

nslog(@"str2 = %@",str2);

nsstring

*path2 = @"";//網路上的一張的位址

nsurl *url = [nsurl

urlwithstring:path2];//

將字串轉換成

url

nsdata *imgdata = [nsdata

datawithcontentsofurl:url];

uiimage *img = [uiimage

imagewithdata:imgdata];

[imgdata 

writetofile

:@"/users/1010/desktop/logo.gif"

atomically

:yes

]; //nsdata

雖然是二進位制的,但是可以寫入到檔案中

cgrect r = cgrectmake(100, 100, 100, 100);//ios

中的螢幕是絕對定位

uiimageview *_imgview = [[uiimageview

alloc]initwithframe:r];

_imgview.image = img;

[self.window

addsubview:_imgview];

//檔案管理

nsfilemanager*fm = [nsfilemanager

defaultmanager

];//

檔案管理工具是乙個單例

//   [fm createdirectoryatpath:@"/users/1010/desktop/abc/123" withintermediatedirectories:yes attributes:nil error:nil];

//withintermediatedirectories 是否建立中間目錄123

//attributes檔案袋額屬性,唯讀等等許可權一類的東西

//  [fm createfileatpath:@"/users/1010/desktop/abc/123/logo.gif" contents:imgdata attributes:nil];//在某個路徑下建立內容為.....的檔案

// [fm removeitematpath:@"/users/1010/desktop/abc/123/logo.gif" error:nil];

nsstring

*oripath = @"/users/1010/desktop/abc/123/logo.gif";

nsstring

*targetpath = @"/users/1010/desktop/abc/logo.gif";

[fm moveitematpath:oripath topath:targetpath error:nil];//

nsstring

*bundlestr = [[

nsbundle

mainbundle] pathforresource:

@"abc"

oftype:

@"txt"

];//mainbundle

這是乙個單例

nslog(@"bundlestr = %@",bundlestr);

1 檔案測試 2 檔案操作

1 檔案測試函式 2 檔案操作 新建檔案 fopen filename,w 以 寫 的方式開啟乙個不存在的檔案,就會新建該檔案 檔案刪除 unlink 檔案複製 copy filename,aaa bb.txt 盡量使用 和相對路徑,因為linux只認 也沒有磁碟分割槽,而windows 和 都認 ...

2012 6 6檔案操作

path 目錄和檔案操作的命名控制項system.io string changeextension string path,string extension 修改檔案的字尾,修改 支援字串層面的,沒有真的給檔案改名 將兩個路徑合成乙個路徑,比用 好,可以方便解決不加斜線的問題,自動處理路徑分隔符的...

12 檔案操作

可以採用如下兩種方法記錄多個相關 字串1.採用二維陣列 這種方法記錄相關字串會造成內容浪費 而且儲存區使用不夠靈活 2.採用指標陣列 這種方法記錄相關字串不會浪費記憶體而且 儲存區的使用比較靈活 記錄普通變數位址的指標叫一級指標 記錄一級指標變數位址的指標叫二級指標 二級指標可以和指標陣列 記錄第乙...