asp教程.net c#檔案讀取文本執行個體函數
下面的程式碼範例讀取整個檔案,並在檢測到檔案尾時發出通知。
[c#]
using system;
using system.io;
public class textfromfile {
private const string file_name = "myfile.txt";
public static void main(string[] args) {
if (!file.exists(file_name)) {
console.writeline("{0} does not exist!", file_name);
return;
}
streamreader sr = file.opentext(file_name);
string input;
while ((input=sr.readline())!=null) {
console.writeline(input);
}
console.writeline ("the end of the stream has been reached.");
sr.close();
}
}
此代碼通過調用 file.opentext 建立一個指向 myfile.txt 的 streamreader。streamreader.readline 將每一行都返回為一個字串。當沒有要讀取的字元時,一條訊息顯示該情況,然後流關閉。
向檔案寫入文本
下面的程式碼範例建立一個新文字檔並向其寫入一個字串。
[c#]
using system;
using system.io;
public class texttofile {
private const string file_name = "myfile.txt";
public static void main(string[] args) {
if (file.exists(file_name)) {
console.writeline("{0} already exists!", file_name);
return;
}
streamwriter sr = file.createtext(file_name);
sr.writeline ("this is my file.");
sr.writeline ("i can write ints {0} or floats {1}, and so on.",
1, 4.2);
sr.close();
}
}
如何把streamreader關聯到filestream上。其優點是可以顯式指定是否建立檔案和共用許可,如果直接把streamreader關聯到檔案上,就不能這麼做:
filestream fs = new filestream(@"c:"my documents"readme.txt",
filemode.open, fileaccess.read, fileshare.none);
streamreader sr = new streamreader(fs);
對於本例,指定streamreader尋找位元組碼標記,以確定使用了什麼編碼方法,以後的樣本也是這樣,從一個fileinfo執行個體中獲得streamreader:
fileinfo myfile = new fileinfo(@"c:"my documents"readme.txt");
streamreader sr = myfile.opentext();
與filestream一樣,應在使用後關閉streamreader。如果沒有這樣做,就會致使檔案一直鎖定,因此不能執行其他過程(除非使用filestream構造streamreader和特定的fileshare. sharereadwrite):
sr.close();
介紹完執行個體化streamreader後,就可以用該執行個體作一些工作了。與filestream一樣,我們僅指出可以用於讀取資料的許多方式,您應在sdk文檔說明書中查閱其他不太常用的streamreader方法。
所使用的最簡單的方式是readline(),該方法一次讀取一行,但返回的字串中不包括標記該行結束的斷行符號分行符號:
string nextline = sr.readline();
另外,還可以在一個字串中提取檔案的所有剩餘內容(嚴格地說,是流的全部剩餘內容):
string restofstream = sr.readtoend();
可以唯讀取一個字元:
int nextchar = sr.read();
read()的重載方法可以把返回的字元轉換為一個整數,如果到達流的尾端,就返回-1。
最後,可以用一個位移值,把給定個數的字元讀到數組中:
// to read 100 characters in.
int nchars = 100;
char [] chararray = new char[nchars];
int ncharsread = sr.read(chararray, 0, nchars);
如果要求讀取的字元數多於檔案中剩餘的字元數,ncharsread應小於nchars 。
2. streamwriter類
streamwriter類的工作方式與streamreader的類似,但streamwriter只能用於寫入檔案(或另一個流)。構造streamwriter的方法包括:
streamwriter sw = new streamwriter(@"c:"my documents"readme.txt");
上面的代碼使用了utf8編碼方法,.net把這種編碼方法設定為預設的編碼方法。如果要指定其他的編碼方法:
streamwriter sw = new streamwriter(@"c:"my documents"readme.txt", true,
encoding.ascii);
在這個建構函式中,第二個參數是boolean型,表示檔案是否應以追加方式開啟。建構函式的參數不能僅是一個檔案名稱和一個編碼類別。
當然,可以把streamwriter關聯到一個檔案流上,以獲得開啟檔案的更多控制選項:
filestream fs = new filestream(@"c:"my documents"readme.txt",
filemode.createnew, fileaccess.write, fileshare.read);
streamwriter sw = new streamwriter(fs);
fileinfo不執行返回streamwriter的任何方法。
另外,如果要建立一個新檔案,並開始給它寫入資料,可以使用下面的代碼:
fileinfo myfile = new fileinfo(@"c:"my documents"newfile.txt");
streamwriter sw = myfile.createtext();
與其他流類一樣,在使用完後,要關閉streamwriter:
sw.close();
寫入流可以使用streamwriter.write()的4個重載方法來完成。最簡單的方式是寫入一個流,後面加上一個斷行符號分行符號:
string nextline = "groovy line";
sw.write(nextline);
也可以寫入一個字元:
char nextchar = ~a~;
sw.write(nextchar);
也可以寫入一個字元數組:
char [] chararray = new char[100];
// initialize these characters
sw.write(chararray);
甚至可以寫入字元數組的一部分:
int ncharstowrite = 50;
int startatlocation = 25;
char [] chararray = new char[100];
// initialize these characters
sw.write(chararray, startatlocation, ncharstowrite);
3.readwritetext樣本
readwritetext樣本說明了streamreader和streamwriter類的用法。它非常類似於前面的readbinaryfile樣本,但假定要讀取的檔案是一個文字檔,並顯示其內容。它還可以儲存檔案(包括在文字框中對文本進行的修改)。它將以unicode格式儲存檔案。
圖30-9所示的readwritetext用於顯示前面的newfile.aspx檔案。但這次讀取內容會更容易一些。
這裡不打算介紹給開啟檔案對話方塊添加事件處理常式的詳細內容,因為它們基本上與前面的binaryfilereader樣本相同。與這個樣本相同,開啟一個新檔案,將調用displayfile()方法。其惟一的區別是displayfile的執行方式,本例有一個儲存檔案的選項。這由另一個功能表項目save來表示,這個選項的處理常式調用我們添加到代碼中的另一個方法savefile()(