如何在C#中打开大文本文件 [英] How to open a large text file in C#
本文介绍了如何在C#中打开大文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文本文件,其中包含约100000篇文章. 文件的结构为:
I have a text file that contains about 100000 articles. The structure of file is:
.Document ID 42944-YEAR:5
.Date 03\08\11
.Cat political
Article Content 1
.Document ID 42945-YEAR:5
.Date 03\08\11
.Cat political
Article Content 2
我想用c#打开此文件以逐行处理它. 我尝试了这段代码:
I want to open this file in c# for processing it line by line. I tried this code:
String[] FileLines = File.ReadAllText(
TB_SourceFile.Text).Split(Environment.NewLine.ToCharArray());
但是它说:
类型异常 "System.OutOfMemoryException"原为 抛出.
Exception of type 'System.OutOfMemoryException' was thrown.
问题是如何打开此文件并逐行读取.
The question is How can I open this file and read it line by line.
- 文件大小:564 MB(591,886,626字节)
- 文件编码:UTF-8
- 文件包含Unicode字符.
推荐答案
You can open the file and read it as a stream rather than loading everything into memory all at once.
从MSDN:
using System;
using System.IO;
class Test
{
public static void Main()
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}
这篇关于如何在C#中打开大文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文