![](/img/trans.png)
[英]'System.OutOfMemoryException' when using Font from local system cache
[英]'System.OutOfMemoryException' when using ReadAllText() method
我有一個制表符分隔文件,其中包含8,000,000+行,其中包含一些流氓選項卡。
例如:
a->b->c->d
a->b->c->-->-->--d
a->b->c->d
a->b->c->d
我有一種方法來糾正流氓選項卡(3個選項卡到1個選項卡),如下所示:
string text = File.ReadAllText(filePath);
text = text.Replace("\t\t\t", "\t");
File.WriteAllText(filePath, text);
上面的代碼塊產生以下錯誤:
mscorlib.dll中發生了'System.OutOfMemoryException'類型的未處理異常
如何一次只讀取和寫入一行,以使整個文件不位於內存中?
File.ReadLines
為您提供了一個惰性IEnumerable<string>
。 您可以改為遍歷該對象,一次僅加載一行。
不過,您需要寫入的文件與讀取的文件不同。 完成后,您可以刪除/重命名。
這是處理文件的單線:
File.WriteAllLines(outputFile,
File.ReadLines(inputFile).
Select(t => t.Replace("\t\t\t", "\t"))
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.