随风而行

^o^ 格言:相信没有做不到的事情,只有想不到的事情.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Read and Write 单一文件

Posted on 2009-02-23 14:11  随风而行  阅读(112)  评论(0编辑  收藏  举报
private void operateFlatFile()
{
    string path;
    FileStream fs;
    byte[] _read, _write;
    int start, end;

    start = 0;
    end = 50;

 

    // read
    path = Server.MapPath("http://www.cnblogs.com/document/encode.txt");
    fs = new FileStream(path, FileMode.Open);
    _read = new byte[end];

    fs.Read(_read, start, end);
    fs.Close();

 

    // print
    Encoding ur = Encoding.GetEncoding("gb2312");
    string content = ur.GetString(_read);
    Response.Write(content + "<br>");

 

    // write
    path = Server.MapPath("http://www.cnblogs.com/document/new.txt");
    fs = new FileStream(path, FileMode.OpenOrCreate) ;

    Encoding uw = Encoding.GetEncoding("utf-8") ;
    _write = uw.GetBytes(content);

    fs.Write(_write, start, _write.Length);
    fs.Close();

 

    // show message

    Response.Write( "succ.");
}