当前位置:编程学习 > C#/ASP.NET >>

io流

请高手指教 怎么写io流  主要功能读写txt文件 谢谢 --------------------编程问答--------------------
 protected void Page_Load(object sender, EventArgs e)
      {
          if (!this.IsPostBack)
          {
              string Path = Server.MapPath("2.txt");//路径
              insertStr(Path);
          }
      }
     //读取文件
      public void insertStr(string Path)
      {
          string strLine = "";
          int i = 0;
          try
          {
              StreamReader sr = new StreamReader(Path, Encoding.GetEncoding("GB2312"));

              strLine = sr.ReadLine();

              while (strLine != null)
              {
                  Response.Write(strLine + "<br>");
                  strLine = sr.ReadLine();
                  i++;    
              }
              Response.Write(i);
              sr.Dispose();
              sr.Close();
          }
          catch 
          { 
          }

       
      }

--------------------编程问答-------------------- using System.Text;
using System.IO;
--------------------编程问答--------------------  public void wirte(string path)
        {
            string context = "";
            Stream s =null;
            StreamWriter sw =null;
            try
            {
                 s = File.Open(path, FileMode.OpenOrCreate);
                 sw = new StreamWriter(s, Encoding.GetEncoding("GB2312"));
                sw.Write(context);
            }
            catch(Exception)
            {

            }
            finally
            {
                s.Dispose();
                s.Close();
                sw.Dispose();
                sw.Close();
            }

        } --------------------编程问答-------------------- baidu一下就有答案。

主要的类就是那个几个: Stream相关的 --------------------编程问答-------------------- 0.0.求正解 --------------------编程问答-------------------- MSDN中就有详细的示例:http://msdn.microsoft.com/en-us/library/db5x7c0d.aspx
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,