我正在开发一个类,用于编辑非常大的文本文件(4Gb以上)。 尽管这可能听起来有点愚蠢,但是我不明白如何在流中修改文本。以下是我的代码:
public long Replace(String text1, String text2)
{
long replaceCount = 0;
currentFileStream = File.Open(CurrentFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
using (BufferedStream bs = new BufferedStream(currentFileStream))
using (StreamReader sr = new StreamReader(bs))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Contains(text1))
{
line.Replace(text1, text2);
// Here I should save changed line
replaceCount++;
}
}
}
return replaceCount;
}
StringBuilder
,显然无法处理大文件)。我删除了我的回答。 - Alexei Levenkov