2016-04-21 33 views
1

我試圖解決目前這個問題readling整個文件:如何編輯一個文本文件,而不用一次

編寫一個程序,替換子「開始」,在「完成」的每一次出現一個文本文件。你可以重寫程序來替換整個單詞嗎?程序是否適用於大文件(例如800 MB)?

我一直在努力去做,但顯然你不能同時閱讀和寫作。 如果有人可以看我的代碼,並幫助我,它會很棒。它拋出一個異常:

The process cannot access the file 'C:\Users\Nate\Documents\Visual Studio 2015\Projects\Chapter 15\Chapter 15 Question 7\Chapter 15 Question 7\TextFile.txt' because it is being used by another process.

你不必給我答案直線,而是告訴我過程。謝謝!

這裏是我的代碼目前

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 


namespace Chapter_15_Question_7 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     StreamReader reader = new StreamReader(
      @"C:\Users\Nate\Documents\Visual Studio 2015\Projects\Chapter 15\Chapter 15 Question 7\Chapter 15 Question 7\TextFile.txt"); 

     StreamWriter writer = new StreamWriter(
      @"C:\Users\Nate\Documents\Visual Studio 2015\Projects\Chapter 15\Chapter 15 Question 7\Chapter 15 Question 7\TextFile.txt"); 
     using (writer) 
     { 
      using (reader) 
      { 
       string line = reader.ReadLine(); 
       while (line != null) 
       { 
        line.Replace("start", "finish"); 
        writer.WriteLine(line); 
        line = reader.ReadLine(); 
       } 
      } 
     } 
    } 
} 

}

+0

你是不是讀整個文件一次,你是逐行讀取它行。這是立即閱讀:http://stackoverflow.com/a/13509665/169714大文件,考慮使用'BufferedStream' http://stackoverflow.com/a/9643111/169714 –

+0

你也可以使用[文件.ReadAllLines](https://msdn.microsoft.com/en-us/library/s2tte0y1(v = vs.110)的.aspx)。 –

+0

這一點,我不是一口氣讀完整個文件。這是挑戰。如果文件足夠大,我一次讀完讀取整個文件就會導致異常。我試圖找到不同的時刻atm – Nate

回答

3

沒有測試它。但這是來自我在評論中發佈的鏈接。

我會做的是我做一個臨時文件,並逐行寫入,然後用新的替換舊的文本文件。

事情是這樣的:

string path = @"C:\Users\Nate\Documents\Visual Studio 2015\Projects\Chapter 15\Chapter 15 Question 7\Chapter 15 Question 7\TextFile.txt"; 
string pathTmp = @"C:\Users\Nate\Documents\Visual Studio 2015\Projects\Chapter 15\Chapter 15 Question 7\Chapter 15 Question 7\TextFile-tmp.txt"; 

using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) 
{ 
    using (StreamReader sr = new StreamReader(fs)) 
    {  
     string line; 
     while ((line = sr.ReadLine()) != null) 
     {     
      using (StreamWriter writer = new StreamWriter(pathTmp)) 
      { 
       writer.WriteLine(line.Replace("start", "finish")); 
      } 
     } 
    } 
} 
File.Delete(path); 
File.Move(pathTmp, path); 
+0

我試過你的方法,但它拋出一個參數異常在 'var sw = new System.IO.StreamWriter(bs);' 它說streamwriter是不可寫的 – Nate

+0

使用'BufferedStream'真的很可疑,因爲'FileStream '已被默認緩衝。 「BufferedStream」的使用非常過時。 –

+0

你應該刪除這個答案的第一部分,因爲這是無稽之談。第二部分是好的。另外,在第二部分中擺脫使用'BufferedStream' - 這已經過時了。 –

5

我一直在試圖做到這一點,但顯然你不能閱讀,並在同一時間寫。

的技巧來解決這個問題很簡單:

  • 打開在同一文件夾作爲原始寫入一個臨時文件,
  • 閱讀原線,由線,完成替換操作,並將它們寫入臨時文件
  • 關閉原始文件和臨時文件
  • 代替原始文件的副本進行臨時文件

您已經在逐行讀取您的文件,因此您只需將writer更改爲使用不同的文件名,並在循環結束後添加調用以移動文件。

+0

如何打開一個臨時文件。是否只是打開一個新的寫入流?並在兩者上使用'File.Delete()'方法? – Nate

+0

是和否 - 你只需打開一個新的流,但你只會刪除第一個文件並重命名臨時文件... – Aconcagua

+0

@Nate Use ['GetRandomFileName'](https://msdn.microsoft.com/en- us/library/system.io.path.getrandomfilename(v = vs.110).aspx)創建臨時文件的名稱。 – dasblinkenlight

0
StreamWriter writer = new StreamWriter(
     @"C:\Users\Nate\Documents\Visual Studio 2015\Projects\Chapter 15\Chapter 15 Question 7\Chapter 15 Question 7\TextFile.txt"); 
    using (writer) 
    { 
     using (reader) 
     { 
      string line = reader.ReadLine(); 
      while (line != null) 
      { 
       string myNewLine=line.Replace("start", "finish"); 
       writer.WriteLine(myNewLine); 
      } 
     } 
    } 
} 

}

0

我認爲這將是一個更簡潔的方法:

string fileToUpdate = @"C:\Users\Nate\Documents\Visual Studio 2015\Projects\Chapter 15\Chapter 15 Question 7\Chapter 15 Question 7\TextFile.txt"; 
string tempFile = fileToUpdate + ".tmp"; 

File.WriteAllLines(tempFile, 
    File.ReadLines(fileToUpdate) 
    .Select(line => line.Replace("start", "finish"))); 

File.Delete(fileToUpdate); 
File.Move(tempFile, fileToUpdate); 
相關問題