我需要編寫應用程序來刪除非常大的XML文件(大約3,5 GB)中的特定文本行。刪除XML文件中的文本行C#.NET
我寫了這個代碼:
string directoryPath;
OpenFileDialog ofd = new OpenFileDialog();
private void button1_Click(object sender, EventArgs e)
{
ofd.Filter = "XML|*.xml";
if (ofd.ShowDialog() == DialogResult.OK)
{
directoryPath = Path.GetDirectoryName(ofd.FileName);
textBox2.Text = directoryPath;
textBox1.Text = ofd.SafeFileName;
}
}
private void Replace()
{
StreamReader readerFile = new StreamReader(ofd.FileName, System.Text.Encoding.UTF8);
while (!readerFile.EndOfStream)
{
string stringReplaced;
string replaceResult = textBox2.Text + "\\" + "replace_results";
Directory.CreateDirectory(replaceResult);
StreamWriter writerFile = new StreamWriter(replaceResult + "\\" + textBox1.Text, true);
StringBuilder sb = new StringBuilder();
char[] buff = new char[10 * 1024 * 1024];
int xx = readerFile.ReadBlock(buff, 0, buff.Length);
sb.Append(buff);
stringReplaced = sb.ToString();
stringReplaced = stringReplaced.Replace("line to remove", string.Empty);
writerFile.WriteLine(stringReplaced);
writerFile.Close();
writerFile.Dispose();
stringReplaced = null;
sb = null;
}
readerFile.Close();
readerFile.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
if (!backgroundWorker1.IsBusy)
{
backgroundWorker1.RunWorkerAsync();
toolStripStatusLabel1.Text = "Replacing in progress...";
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
try
{
Replace();
toolStripStatusLabel1.Text = "Replacing complete!";
}
catch
{
toolStripStatusLabel1.Text = "Error! Replacing aborted!";
}
}
}
它的工作原理,但不是很好,因爲新的文件(後刪除線)比原來的文件,並在新文件的末尾添加一些垃圾更大(大量的點),截圖:
https://images81.fotosik.pl/615/873833aa0e23b36f.jpg
如何解決我的代碼,使新的文件一樣的舊文件,只是沒有具體的線路?
我怎麼只能行做這項工作沒有出內存異常?你能給我代碼嗎? :) – gos
@gos:中央循環應該是:讀取行,進程,寫入行。記憶中一次只有一行。 – Richard
@gos:查看擴展的答案。 – Richard