asp. net下使用foreach簡化文字檔的訪問。

來源:互聯網
上載者:User

很多時候,我們總是按照行的方式訪問文字檔,使用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);
}
}
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.