使用foreach简化文本文件的访问。
2004-08-26 11:31
197 查看
很多时候,我们总是按照行的方式访问文本文件,使用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);
}
}
}
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);
}
}
}
相关文章推荐
- 使用foreach简化文本文件的访问
- asp. net下使用foreach简化文本文件的访问。
- asp. net下使用foreach简化文本文件的访问。
- spring与jpa整合 简化persistence.xml配置文件 使用属性文件 数据源dbcp访问数据库
- 使用osgi.util.NLS简化资源文件访问
- 如何:使用 foreach 访问集合类(C# 编程指南)
- learn opencv-使用forEach进行并行像素访问
- C#如何实现自定义集合的IEnumerable 和 IEnumerator 接口并使用foreach访问自定义集合
- C#系列教程——使用 foreach 访问命令行参数
- 微博开发原理简化android使用http访问php获取json
- 《万能数据库查询分析器》实现使用SQL语句直接高效地访问文本文件
- 动态创建文本文件并写入数据 避免正由另一进程使用,因此该进程无法访问该文件。的问题
- IEnumerable 接口---实现该接口里的成员的类,可以使用foreach循环访问集合
- Spring Boot-使用Spring Data JPA简化数据库访问
- 合理使用“.NET扩展方法”来简化代码(例子:空值判断,利用扩展方法实现LINQ操作符ForEach)
- 《万能数据库查询分析器》实现使用SQL语句直接高效地访问文本文件
- 《万能数据库查询分析器》实现使用SQL语句直接高效地访问文本文件
- [译文] 使用JNA来简化对本地代码的访问[一]
- [译文] 使用JNA来简化对本地代码的访问[二]
- 如何使用 foreach 访问集合类