欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp. net下使用foreach簡(jiǎn)化文本文件的訪問(wèn)。

 更新時(shí)間:2007年04月28日 00:00:00   作者:  
       很多時(shí)候,我們總是按照行的方式訪問(wèn)文本文件,使用foreach語(yǔ)句能夠極大地簡(jiǎn)化訪問(wèn)邏輯:例如: 
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(); 
    } 
  } 

測(cè)試代碼: 
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在自定義文本框中實(shí)現(xiàn)輸入法跟隨光標(biāo)

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

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

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

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

    .NET實(shí)現(xiàn)倉(cāng)儲(chǔ)Repository(AI)的操作方法

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

    asp.net獲取真實(shí)ip的方法

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

    Visual Studio 2010配置OpenCV的方法

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

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

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

    Entity Framework使用LINQ操作實(shí)體

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

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

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

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

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

    ASP.NET Eval 求值運(yùn)算的一些用法

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

最新評(píng)論