本文转至:http://www.cnblogs.com/huaxiaoyao/p/4051832.html
做BI开发测试的时候,有可能面对source文件数GB的情况,如果使用一般的文本编辑器,则会卡死,或要等很久才能显示出来。
有时候,我们使用ascii(01)或ascii(02)作为行或列的分隔符,这样的临时文件用于导数据到DB,如果文件导入过程中有错误,需要查看文件 的时候,普通的编辑器不支持换行,则会很恐怖。
在这种情况下,作了以下的C#读取大文件的测试示例:
private IEnumerator<string> ReadLines(string filename)
{
/*
string line;
using (TextReader reader = File.OpenText(filename))
{
while ((line = reader.ReadLine()) != null)
yield return line;
}
*/
StringBuilder sb = new StringBuilder();
using (FileStream fs = File.OpenRead(filename))
{
int b = 0;
while ((b=fs.ReadByte())!=-1)
{
//textbox3 store the row terminator
if (b.ToString() == textBox3.Text.Trim())
{
yield return sb.ToString();
sb.Clear();
}
else
sb.Append(UnicodeEncoding.ASCII.GetString(new byte[] { byte.Parse(b.ToString()) }));
}
}
}
版权声明:本作品系原创,版权归码友网所有,如未经许可,禁止任何形式转载,违者必究。
发表评论
登录用户才能发表评论, 请 登 录 或者 注册