寫入 讀取檔案

2021-06-02 23:06:22 字數 1805 閱讀 9434

file類下面的方法:

string str =file.readalltext("aa.txt", system.text.encoding.getencoding("utf-8"));

//讀取檔案:

console.writeline(str);

console.read();

filestream類下面的方法:

//在當前目錄建立乙個test.txt檔案,該檔案具有讀寫許可權

filestream filestream = new filestream("test.txt", filemode.create, fileaccess.readwrite);

//建立乙個資料流寫入器,和開啟的檔案關聯

streamwriter writer = new streamwriter(filestream);

//以文字的方式寫入乙個檔案

writer.writeline("hello world!");

writer.writeline("資料流工具欄的時刻");

//沖刷資料(將資料真正的寫入檔案)

writer.flush();

//以文字的方式讀取資料

//建立乙個資料流讀取器,開啟檔案關聯

streamreader reader = new streamreader(filestream);

//吧檔案的指標重新指定到檔案的開始

reader.basestream.seek(0, seekorigin.begin);

//列印提示資訊

console.writeline("以文字的方式讀取檔案資訊");

//列印檔案內容

string s1;

while ((s1 = reader.readline()) != null)

console.writeline();

//以文字讀取檔案結束

//以二進位制讀取檔案

//建立乙個二進位製流讀取器,開啟檔案關聯

binaryreader bityreader = new binaryreader(filestream);

//把檔案指標重新指定到檔案的開始

bityreader.basestream.seek(0, seekorigin.begin);

//列印提示新題『

console.writeline("以二進位制的方式讀取檔案");

//列印檔案內容

byte b;

while (bityreader.peekchar() > -1)

", b.tostring());

console.writeline(".");

}else

}console.writeline("\n");

//以二進位制方式讀取檔案結束

writer.close();

reader.close();

bityreader.close();

//讀取檔案屬性,列印提示資訊

console.writeline("讀取檔案屬性");

fileinfo info = new fileinfo("test.txt");

console.writeline("檔名稱為", info.name);

console.writeline("檔名(含路徑)", info.fullname);

console.writeline("檔案大小(bytes)", info.length);

console.writeline("檔案建立時間", info.creationtime);

console.read();

讀取寫入檔案

file1 open c users administrator desktop text 成績.txt w encoding gbk file1.write 羅恩 23 35 44 哈利 60 77 68 88 90 馬赫 97 99 89 91 95 99 媽富爾 100 85 90 file1...

檔案讀取 寫入

這是我為了記錄知識隨寫,不喜勿噴 步驟 1.建立檔案流 filestream fs new filestream 路徑名稱 filemethod.提交方式,fileaccess.獲取方式 2.建立讀寫器 文文檔案讀 streamreader sr new streamreader fs 二進位制檔案...

讀取寫入ini檔案

1.所用的winapi函式原型為 其中各引數的意義 lpctstr lpstring 是鍵值,也就是變數的值,不過必須為lpctstr型或cstring型的.lpctstr lpfilename 是完整的ini檔名.2.具體使用方法 設現有一名學生,需把他的姓名和年齡寫入 c stud studen...