一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

asp. net下使用foreach簡化文本文件的訪問。

       很多時候,我們總是按照行的方式訪問文本文件,使用foreach語句能夠極大地簡化訪問邏輯:例如: 
foreach (string line in new LineReader(”c:/abc.txt”)) 
  Console.WriteLine(line); 
完整代碼如下: 
using System; 
using System.IO; 
using System.Text; 
using System.Collections; 
namespace Forks.Utils.IO 

    public struct LineReader : IDisposable 
    { 
    public LineReader(string file, Encoding encoding) : this(file, encoding, false) 
        { 
    } 
    public LineReader(string file, Encoding encoding, bool ignoreBlankLines) : this(new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read), encoding, ignoreBlankLines) 
    { 
    } 
    public LineReader(Stream stream, Encoding encoding) : this(stream, encoding, false) 
    { 
    } 
    public LineReader(Stream stream, Encoding encoding, bool ignoreBlankLines) : this(new StreamReader(stream, encoding), ignoreBlankLines) 
    { 
    } 
    public LineReader(TextReader reader) : this(reader, false) 
    { 
    } 
    TextReader mReader; 
    bool mIgnoreBlankLines; 
    public LineReader(TextReader reader, bool ignoreBlankLines) 
    { 
      mReader = reader; 
      mIgnoreBlankLines = ignoreBlankLines; 
      mCurrent = null; 
    } 
    public LineReader GetEnumerator() 
    { 
      return this; 
    } 
    public void Reset() 
    { 
      throw new NotSupportedException("LineReaderÖ»ÄܶÁȡһ´Î"); 
    } 
    string mCurrent; 
    public string Current 
    { 
      get 
      { 
        return mCurrent; 
      } 
    } 
    public bool MoveNext() 
    { 
      do 
      { 
        mCurrent = mReader.ReadLine(); 
      }while (mIgnoreBlankLines && mCurrent != null && mCurrent.Length == 0); 
      return mCurrent != null; 
    } 
    public void Dispose() 
    { 
      mReader.Close(); 
    } 
  } 

測試代碼: 
using System; 
using System.IO; 
using System.Text; 
using NUnit.Framework; 
using Forks.Test; 
namespace Forks.Utils.IO 

  [TestFixture] 
    public class LineReaderTest 
    { 
    const string TestLines = @"abc asd ewr afa e  
  start with blanks 
end with blanks    
ºº×Öabc123!@# 
end of text!"; 
    [Test] 
    public void ReadFromReader() 
    { 
      doTest(new LineReader(new StringReader(TestLines))); 
    } 
    [Test] 
    public void ReadFromFile() 
    { 
      string file = Path.GetTempFileName(); 
      try 
      { 
        StringUtil.SaveToFile(TestLines, file, Encoding.GetEncoding("gb2312")); 
        doTest(new LineReader(file, Encoding.GetEncoding("gb2312"))); 
      } 
      finally 
      { 
        FileUtil.SafeDelete(file); 
      } 
    } 
    [Test] 
    public void ReadFromStream() 
    { 
      string file = Path.GetTempFileName(); 
      try 
      { 
        StringUtil.SaveToFile(TestLines, file, Encoding.GetEncoding("gb2312")); 
        using (Stream stream = new FileStream(file, FileMode.Open)) 
          doTest(new LineReader(stream, Encoding.GetEncoding("gb2312"))); 
      } 
      finally 
      { 
        FileUtil.SafeDelete(file); 
      } 
    } 
    void doTest(LineReader reader) 
    { 
      StringBuilder sb = new StringBuilder(); 
      foreach (string line in reader) 
        sb.Append(line + Environment.NewLine); 
      Assert.AreEqual(TestLines + Environment.NewLine, sb.ToString()); 
    } 
    [Test] 
    public void IgnoreBlankLine() 
    { 
      foreach (string line in new LineReader(new StringReader(TestLines), true)) 
        Assert.IsTrue(line.Length != 0); 
    } 
    } 

AspNet技術asp. net下使用foreach簡化文本文件的訪問。,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 成人在线色视频 | 国产综合激情在线亚洲第一页 | 四虎新网站 | 九九视频国产 | 91在线入口 | 国产精品福利一区二区亚瑟 | 国产a久久精品一区二区三区 | 亚洲综合在线成人一区 | 成人免费在线观看视频 | 国产综合激情在线亚洲第一页 | 91啦在线观看| 婷婷成人丁香五月综合激情 | 视色4se视频在线观看 | 免费国产叼嘿视频在线观看 | 欧美日本一区亚洲欧美一区 | 91精品国产福利尤物免费 | 91精品导航在线网址免费 | 福利在线播放 | 中文字幕色网站 | 青青草伊人久久 | 黄色免费网站在线观看 | 中文字幕区 | 国产精品人人视频 | 中文字幕第一页在线视频 | 国产精选在线视频 | 久久久久久久久毛片精品 | 高清一区二区在线观看 | 欧美性与交视频在线观看 | 久久福利国产 | 成人在线观看视频免费 | 中文字幕色网站 | 最新欧美精品一区二区三区不卡 | 国产三级欧美 | 影音先锋加勒比 | 日韩成人免费一级毛片 | 视频在线观看91 | 国产香蕉久久精品综合网 | 91黄色大片 | 成人精品一区久久久久 | 欧美国产一区二区二区 | 国产自产精品 |