asp.net c#建立的資料檔案進行讀取

來源:互聯網
上載者:User

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; }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.