如何在C#中打开一个大文本文件
发布时间:2020-12-15 20:48:15 所属栏目:百科 来源:网络整理
导读:我有一个包含大约100000篇文章的文本文件. 文件结构是: .Document ID 42944-YEAR:5.Date 03 811.Cat politicalArticle Content 1.Document ID 42945-YEAR:5.Date 03 811.Cat politicalArticle Content 2 我想在c#中打开这个文件,逐行处理它. 我试过这段
我有一个包含大约100000篇文章的文本文件.
文件结构是: .Document ID 42944-YEAR:5 .Date 03 811 .Cat political Article Content 1 .Document ID 42945-YEAR:5 .Date 03 811 .Cat political Article Content 2 我想在c#中打开这个文件,逐行处理它. String[] FileLines = File.ReadAllText( TB_SourceFile.Text).Split(Environment.NewLine.ToCharArray()); 但它说:
问题是如何打开此文件并逐行阅读. >文件大小:564 MB(591,886,626字节) 解决方法
您可以打开文件和
read it as a stream,而不是一次性将所有内容加载到内存中.
来自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); } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |