C 的IO資料操作

2021-10-24 11:45:42 字數 3706 閱讀 9858

如果你要讀取的檔案內容不是很多,

可以使用 file.readalltext(filepath) 或指定編碼方式 file.readalltext(filepath, encoding)的方法。

它們都一次將文字內容全部讀完,並返回乙個包含全部文字內容的字串

string str = file.readalltext(@「c:\temp\ascii.txt」);

//也可以指定編碼方式

string str2 = file.readalltext(@「c:\temp\ascii.txt」, encoding.ascii);

也可以使用方法file.readalllines。該方法返回乙個字串陣列。每一行都是乙個陣列元素。

string strs = file.readalllines(@「c:\temp\ascii.txt」);

//也可以指定編碼方式

string strs2 = file.readalllines(@「c:\temp\ascii.txt」, encoding.ascii);

當文字的內容比較大時,

我們就不要將文字內容一次讀完,而應該採用流(stream)的方式來讀取內容。.net為我們封裝了streamreader類。

初始化streamreader類有很多種方式。

streamreader sr1 = new streamreader(@「c:\temp\utf-8.txt」);

//同樣也可以指定編碼方式

streamreader sr2 = new streamreader(@「c:\temp\utf-8.txt」, encoding.utf8);

filestream fs = new filestream(@「c:\temp\utf-8.txt」, filemode.open, fileaccess.read, fileshare.none);

streamreader sr3 = new streamreader(fs);

streamreader sr4 = new streamreader(fs, encoding.utf8);

fileinfo myfile = new fileinfo(@「c:\temp\utf-8.txt」);

// opentext 建立乙個utf-8 編碼的streamreader物件

streamreader sr5 = myfile.opentext();

// opentext 建立乙個utf-8 編碼的streamreader物件

streamreader sr6 = file.opentext(@「c:\temp\utf-8.txt」);

初始化完成之後,你可以每次讀一行,也可以每次讀乙個字元 ,還可以每次讀幾個字元,甚至也可以一次將所有內容讀完。

// 讀一行

string nextline = sr.readline();

// 讀乙個字元

int nextchar = sr.read();

// 讀100個字元

int nchars = 100;

char chararray = new char[nchars];

int ncharsread = sr.read(chararray, 0, nchars);

// 全部讀完

string restofstream = sr.readtoend();

使用完streamreader之後,不要忘記關閉它:

sr.closee();

streamreader sr = file.opentext(@「c:\temp\ascii.txt」);

string nextline;

while ((nextline = sr.readline()) != null)

sr.close();

如果你要寫入的內容不是很多,

可以使用file.writealltext方法來一次將內容全部寫如檔案。

如果你要將乙個字串的內容寫入檔案,可以用file.writealltext(filepath) 或指定編碼方式 file.writealltext(filepath, encoding)方法。

string str1 = 「good morning!」;

file.writealltext(@「c:\temp\test\ascii.txt」, str1);

// 也可以指定編碼方式

file.writealltext(@「c:\temp\test\ascii-2.txt」, str1, encoding.ascii);

string strs = ;

file.writealllines(@「c:\temp\ascii.txt」, strs);

// 也可以指定編碼方式

file.writealllines(@「c:\temp\ascii-2.txt」, strs, encoding.ascii);

使用file.writealltext或file.writealllines方法時,如果指定的檔案路徑不存在,會建立乙個新檔案;如果檔案已經存在,則會覆蓋原檔案。

當要寫入的內容比較多時,

同樣也要使用流(stream)的方式寫入。.net封裝的類是streamwriter。

初始化streamwriter類同樣有很多方式:

// 如果檔案不存在,建立檔案; 如果存在,覆蓋檔案

streamwriter sw1 = new streamwriter(@「c:\temp\utf-8.txt」);

// filemode.createnew: 如果檔案不存在,建立檔案;如果檔案已經存在,丟擲異常

filestream fs = new filestream(@「c:\temp\utf-8.txt」, filemode.createnew, fileaccess.write, fileshare.read);

// utf-8 為預設編碼

streamwriter sw3 = new streamwriter(fs);

streamwriter sw4 = new streamwriter(fs, encoding.utf8);

// 如果檔案不存在,建立檔案; 如果存在,覆蓋檔案

fileinfo myfile = new fileinfo(@「c:\temp\utf-8.txt」);

streamwriter sw5 = myfile.createtext();

初始化完成後,可以用streamwriter物件一次寫入一行,乙個字元,乙個字元陣列,甚至乙個字元陣列的一部分。

// 寫乙個字元

sw.write(『a』);

// 寫乙個字元陣列

char chararray = new char[100];

// initialize these characters

sw.write(chararray);

// 寫乙個字元陣列的一部分

sw.write(chararray, 10, 15);

同樣,streamwriter物件使用完後,不要忘記關閉。

sw.close();

最後來看乙個完整的使用streamwriter一次寫入一行的例子:

fileinfo myfile = new fileinfo(@「c:\temp\utf-8.txt」);

streamwriter sw = myfile.createtext();

string strs = ;

foreach (var s in strs)

sw.close();

C 常用IO操作

建立資料夾 如果資料夾路徑不存在則建立資料夾 if directory.exists path directory.createdirectory path 遞迴建立資料夾 public void createdir string fullpath view code 刪除整個資料夾 directo...

C 檔案IO操作

3.檔案 a 檔案基本操作 public static filestream create string 給定檔案路徑名,建立檔案,並返回乙個filestream流物件。public static streamwriter createtext string 給定檔案路徑名,以文字的方式建立檔案,並...

C語言檔案IO操作(標準IO)

函式 file fopen const char path,const char mode 引數1 將要開啟的檔案路徑 引數2 開啟檔案的方式 1.r 唯讀的方式開啟 2.w 若檔案不存在則建立檔案,若存在此檔案則清空檔案內容並打卡 3.a 若檔案不存在則建立檔案,若存在則在末尾追加 不會清空原檔案...