.net - 在 .NET 中读取和解析文件 - Performance For Hire

标签 .net performance file

我想要最高效的方式来读取和解析文件。

是否可以在 .NET 中读取文件,但不能将整个文件加载到内存中?即在我解析每一行的内容时逐行加载文件?

XmlTextReader 是将整个文件加载到内存中,还是在读取文件时将文件流式传输到内存中?

最佳答案

您可以使用 StreamReader 类的 ReadLine 方法:

string line;

// Read the file and display it line by line.
System.IO.StreamReader file = 
   new System.IO.StreamReader("c:\\test.txt");

while((line = file.ReadLine()) != null)
{
   Console.WriteLine (line);
}

file.Close();

对于 XML 文件,我会使用 XMLTextReader。看到这个 article on Dr. Dobb's Journal :

" 使用 C# 解析 .NET 中的 XML 文件: .NET 中有五种不同的解析技术,每一种都有自己的优势"

关于.net - 在 .NET 中读取和解析文件 - Performance For Hire,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/247066/

相关文章:

c# - 按日期排序文件

c# - 扫描自定义属性的所有类和方法的最佳实践

sql - 连接表时 PostgreSQL ON 与 WHERE?

java - Android java.io.FileNotFoundException图像打开失败: ENOENT from internal storage

php - 将插入查询从 sql 文件转换为 php 数组

C# WinForms DataGridView - 选定常量行!

c# - 为什么 Monitor.PulseAll 在信号线程中导致 "stepping stair"延迟模式?

c# - 我可以使用 null 条件运算符而不是经典的事件引发模式吗?

java - Log4j2 monitorInterval性能

mysql - 当数据从mysql中删除后,它所占用的空间会发生什么变化?