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

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

主站蜘蛛池模板: 青柠在线视频 | 8X拨牐拨牐X8免费视频8 | 神马电影dy888午夜我不卡 | 国产高清视频在线观看97 | 一个人看的WWW高清电影 | 精品亚洲一区二区三区在线播放 | 中文字幕日本在线mv视频精品 | 丰满饥渴老太性hd | 久久综合香蕉久久久久久久 | 二级特黄绝大片免费视频大片 | ZZoo兽2皇 | 超嫩校花被灌醉在线观看 | 蜜桃传媒一区二区亚洲AV | 中文字幕日本一区 | 亚洲女人网 | 精品少妇爆AV无码专区 | 久久AV亚洲精品一区无码网 | 久久久精品日本一区二区三区 | 亚洲精品福利一区二区在线观看 | 丰满少妇69激懒啪啪无码 | 趁老师睡着吃她的奶水 | 2021精品乱码多人收藏 | 手机在线观看mv网址 | 99精品网站 | 无人区国产片 | 乌克兰少妇大胆大BBW | 亚洲七七久久桃花综合 | 国产精品成人在线播放 | 日本湿姝在线观看 | 一个人的视频全免费在线观看www | 国产激情精品久久久久久碰 | 老司机无码精品A | 全文都是肉高h文 | 青青草偷拍国产亚洲欧洲 | 日本高清不卡一区久久精品 | seyeye在清在线 | 美女禁处受辱漫画 | 国产精品久久久久久久久LI无码 | 日产日韩亚洲欧美综合搜索 | 午夜福利08550 | 快播h网站 |