内のテキスト行を削除し、私は非常に大規模な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中央ループがなければならない:読み取りライン、プロセス、書き込み線。一度に1行しかメモリにはありません。 – Richard
@gos:拡大答えを参照してください。 – Richard