亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2007年04月28日 00:00:00   作者:  
       很多時候,我們總是按照行的方式訪問文本文件,使用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); 
    } 
    } 

相關(guān)文章

  • WPF在自定義文本框中實現(xiàn)輸入法跟隨光標(biāo)

    WPF在自定義文本框中實現(xiàn)輸入法跟隨光標(biāo)

    本文主要為大家介紹了如何在WPF寫一個自定義的文本框,并且能實現(xiàn)讓輸入法跟隨光標(biāo)。文中的示例代碼講解詳細(xì),需要的可以參考一下
    2022-02-02
  • .NET之生成數(shù)據(jù)庫全流程實現(xiàn)

    .NET之生成數(shù)據(jù)庫全流程實現(xiàn)

    這篇文章主要介紹了.NET之生成數(shù)據(jù)庫全流程實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • .NET實現(xiàn)倉儲Repository(AI)的操作方法

    .NET實現(xiàn)倉儲Repository(AI)的操作方法

    倉儲模式是一種在應(yīng)用程序中使用的設(shè)計模式,它將數(shù)據(jù)訪問邏輯與業(yè)務(wù)邏輯分離,通過倉儲接口和倉儲實現(xiàn)類,您可以定義和實現(xiàn)數(shù)據(jù)的增刪改查操作,這篇文章主要介紹了.NET?實現(xiàn)倉儲Repository(AI),需要的朋友可以參考下
    2023-09-09
  • asp.net獲取真實ip的方法

    asp.net獲取真實ip的方法

    這篇文章主要介紹了asp.net獲取真實ip的方法,通過一個簡單的自定義函數(shù)達(dá)到獲取用戶真實IP的功能,非常簡單實用,需要的朋友可以參考下
    2015-07-07
  • Visual Studio 2010配置OpenCV的方法

    Visual Studio 2010配置OpenCV的方法

    這篇文章主要為大家詳細(xì)介紹了Visual Studio 2010配置OpenCV的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • 詳解GridView自帶的編輯刪除更新功能

    詳解GridView自帶的編輯刪除更新功能

    本文主要介紹了GridView自帶的編輯、刪除、更新功能實現(xiàn)的方法。具有一定的參考價值,需要的朋友一起來看下吧
    2016-12-12
  • Entity Framework使用LINQ操作實體

    Entity Framework使用LINQ操作實體

    本文詳細(xì)講解了Entity Framework使用LINQ操作實體的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • sqlserver 批量數(shù)據(jù)替換助手V1.0版發(fā)布

    sqlserver 批量數(shù)據(jù)替換助手V1.0版發(fā)布

    前段時間網(wǎng)站被掛馬,數(shù)據(jù)庫表中很多文本字段都被加上了一段js腳本。修復(fù)完程序漏洞之后便開始著手清理這些被注入的數(shù)據(jù),其間參考了一些網(wǎng)上的方法,大都是寫一個存儲過程進(jìn)行一個表一個表逐一清理。
    2011-10-10
  • ASP.NET?Core集成Apollo(阿波羅)

    ASP.NET?Core集成Apollo(阿波羅)

    這篇文章介紹了ASP.NET?Core集成Apollo的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • ASP.NET Eval 求值運算的一些用法

    ASP.NET Eval 求值運算的一些用法

    ASP.NET Eval 求值運算的一些用法,需要的朋友可以參考下。
    2011-10-10

最新評論