asp教程.net c#建立的資料檔案進行讀取
<% @ webhandler language="c#" class="averagehandler" %>
using system;
using system.web;
public class averagehandler : ihttphandler
{
public bool isreusable
{ get { return true; } }
public void processrequest(httpcontext ctx)
{
ctx.response.write("hello");
}
}
cs檔案
using system.web
public sealed class textbuilder : ihttphandler
{
public void processrequest(httpcontext context)
{
context.response.clearcontent();
context.response.contenttype = "text/plain";
context.response.write("hello world");
context.response.end();
}
public bool isreusable
{
get { return true; }
}
}
方法二
binarywriter 和 binaryreader 類用於讀取和寫入資料,而不是字串。下面的程式碼範例示範了向新的、空檔案流 (test.data) 寫入資料及從該檔案讀取資料。在目前的目錄中建立了資料檔案之後,也就同時建立了相關的 binarywriter 和 binaryreader,binarywriter 用於向 test.data 寫入整數 0 到 10,test.data 在檔案尾留下了一個檔案指標。在將檔案指標設定回初始位置後,binaryreader 讀出指定的內容。
[c#]
using system;
using system.io;
class mystream {
private const string file_name = "test.data";
public static void main(string[] args) {
// create the new, empty data file.
if (file.exists(file_name)) {
console.writeline("{0} already exists!", file_name);
return;
}
filestream fs = new filestream(file_name, filemode.createnew);
// create the writer for data.
binarywriter w = new binarywriter(fs);
// write data to test.data.
for (int i = 0; i < 11; i++) {
w.write( (int) i);
}
w.close();
fs.close();
// create the reader for data.
fs = new filestream(file_name, filemode.open, fileaccess.read);
binaryreader r = new binaryreader(fs);
// read data from test.data.
for (int i = 0; i < 11; i++) {
console.writeline(r.readint32());
w.close();
}
}
}
如果 test.data 已存在於目前的目錄中,則引發一個 ioexception。始終使用 filemode.create 建立新檔案,而不引發 ioexception。
方法四
using system.web
public sealed class textbuilder : ihttphandler
{
public void processrequest(httpcontext context)
{
context.response.clearcontent();
context.response.contenttype = "text/plain";
context.response.write("hello world");
context.response.end();
}
public bool isreusable
{
get { return true; }
}
}