将大文本文件输出解析为另一个文本文件

Slo*_*nio 2 c# linq parsing text-files

我想解析一个大文本文件,如果该行包含某个子字符串,则将该行附加到我的新文本文件中.我需要内存使用率最低的解决方案,这是我到目前为止,评论是我需要帮助添加:

.
.
.
if (File.ReadLines(filepath).Any(line => line.Contains(myXML.searchSTRING)))
{

// code to grab that line and append it to the a new text file 
// if new text file doesn't exist then create it.
// All text files im parsing have the same header, I want to grab
// the third line and use it as my new text file header. 
// Only write the header once, I do not want it written every time a new 
// text file is opened for parsing 

}
Run Code Online (Sandbox Code Playgroud)

sam*_*son 7

试试:

var count = 1;
File.WriteAllLines(newFilePath, 
  File.ReadLines(filepath)
  .Where(count++ == 3 || l => l.Contains(myXML.searchSTRING))
);
Run Code Online (Sandbox Code Playgroud)

双方WriteAllLines()ReadLines()用枚举,所以应该有相对较低的内存使用情况.

我不知道你怎么知道只写一次标题,这取决于你如何打开可用的文件列表.他们在一个阵列?如果是这样,将File.WriteAllLines调用包装在该数组周围的foreach循环中.