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

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

主站蜘蛛池模板: 伊人大香人妻在线播放 | 伊人色啪啪天天综合婷婷 | 欧美熟妇VIVOE精品 | 伊人色综合久久天天网 | 蜜臀AV熟女人妻中文字幕 | 日本漫画大全无翼乌 | 日日啪无需播放器 | qvod免费电影 | 国产色婷婷精品人妻蜜桃成熟时 | 999在线观看精品免费 | 99国产精品人妻无码免费 | 在线播放性xxx欧美 在线播放午夜理论片 | 久久AV国产麻豆HD真实乱 | 国产不卡免费 | 久久中文字幕无线观看 | 少妇精品无码一区二区三区 | 伊人久久大香线蕉综合色啪 | 嫩草伊人久久精品少妇AV网站 | 亚洲不卡高清免v无码屋 | 亚洲国产女人aaa毛片在线 | 戳女人屁股流水羞羞漫画 | 一个人免费播放高清在线观看 | 色吧.com| 亚洲日本欧美国产在线视 | 24小时日本高清免费看 | 四虎永久免费 | 亚洲区视频在线观看 | 国产精品一区二区资源 | 东京热无码中文字幕av专区 | 久久视频这里只精品6国产 久久视频在线视频观品15 | 越南女 黑人 痛苦 大叫 | 超熟女专门志 | 日本亚洲精品无码区国产电影 | 国产精品大全国产精品 | 秋霞电影在线观看午夜伦 | 广西美女色炮150p图 | 中文字幕国产在线观看 | 亚洲AV国产福利精品在现观看 | 午夜影院和视费x看 | 好男人好资源在线观看免费视频 | 亚洲视频区 |