寫了一個類給自己用

每次寫文件的讀取都要看一下自己寫過的代碼纔可以寫出來,儘管用的再多都還是不記得,寫了一個文件讀寫的類給自己,把讀寫代碼變成了3行!
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace del
{
    class wasterFileRead
    {
      private string FilePath="";
      private Queue<string> Readline = new Queue<string>();
      public bool IsLastLine
      {
          get
          {
              if (linenum==0)
              {
                  return false;
              }
              else
              {
                  return true;
              }
          }
      }
      private int linenum = 0;
      public wasterFileRead(string filep)
        {
            FilePath = filep;
            FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs, Encoding.Default);
            string rl = "";
          while((rl=sr.ReadLine())!=null)
          {
              Readline.Enqueue(rl);
              linenum += 1;
          }
          sr.Close();
          fs.Close();
        }
        public string filePath
        {
            get
            {
                return FilePath;
            }
            set
            {
                FilePath = filePath;
            }
        }
        public string GetOneLine()
        {
            linenum -= 1;
           return Readline.Dequeue();
        }
    }
}
用了這個類之後讀取c盤a.txt就簡化成了3行
wasterFileRead re = new wasterFileRead(@"c:/a.txt");
            while (re.IsLastLine)
            {
                richTextBox1.AppendText(re.GetOneLine()+"\n");
            }
 
IsLastLine就是返回是否最後一行
GetOneLine()依次返回讀取到的行
初始化的時候要帶文件的位置的字符串參數。
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章