天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

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簡化文本文件的訪問。,轉載需保留來源!

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

主站蜘蛛池模板: 3d在线看小舞被躁视频 | 日韩亚洲国产欧美免费观看 | 777ZYZ玖玖资源站最稳定网址 | 精品欧美一区二区三区久久久 | 神马电影我不卡国语版 | 99热只有精品 | 久久777国产线看观看精品卜 | 国产高清视频在线观看97 | 在线观看国产高清免费不卡 | 美女脱衣服搞鸡 | 无码日本亚洲一区久久精品 | 解开白丝老师的短裙猛烈进入 | 成年视频xxxxxx在线 | 国产手机精品一区二区 | 国产学生无码中文视频一区 | 中文字幕a有搜索网站 | 伦理片秋霞免费影院 | 国产亚洲视频在线播放香蕉 | 97视频免费上传播放 | 国产欧美日韩精品a在线观看高清 | 精品亚洲欧美中文字幕在线看 | 国产在线精品亚洲观看不卡欧美 | 欧美性爱 成人 | 伊人久久国产 | 久久是热频国产在线 | 亚洲野狼综合网站 | 久久精品久精品99热 | 欧美一区二区三区男同 | 亚洲免费在线播放 | 一本色道久久综合亚洲精品 | 性色爽爱性色爽爱网站 | 日韩免费一区 | 扒开腿狂躁女人GIF动态图 | 精品国产免费人成视频 | 亚洲国产中文字幕新在线 | 午夜影视免费 | 国产精品婷婷五月久久久久 | 国产无线乱码一区二三区 | 国产69精品久久久久乱码 | 成年人在线免费观看视频网站 | 久久99热狠狠色AV蜜臀 |