2012-08-27 43 views
0

我有這個代碼比較兩個文本文件,並將差異寫入日誌文件,但由於某些原因,即使使用某些行測試log.txt文件有時是空白的開始與*這些不總是寫任何我必須保存文本文件時寫完,雖然這並不能解釋爲什麼有時它的工作原理任何幫助將是巨大的剩餘在緩衝寫入文本文件並不總是工作/保存

private void compare() 
{ 
    string FilePath = @"c:\snapshot\1.txt"; 
    string Filepath2 = @"c:\snapshot\2.txt"; 
    int counter = 0; 
    string line; 
    string line2; 

    var dir = "c:\\snapshot\\log.txt"; 

    using (FileStream fs = File.Create(dir)) 
    { 
    fs.Dispose(); 
    } 

    StreamWriter dest = new StreamWriter(dir); 

    if (File.Exists(FilePath) & File.Exists(Filepath2)) 
    { 
    // Read the file and display it line by line. 
    using (var file = File.OpenText(FilePath)) 
    using (var file2 = File.OpenText(Filepath2)) 
    { 
     while (((line = file.ReadLine()) != null & (line2 = file2.ReadLine()) != null)) 
     { 
     if (line.Contains("*")) 
     { 
      dest.WriteLine(line2); 
     } 
     else if (!line.Contains(line2)) 
     { 
      dest.WriteLine(line2); 
     } 
     counter++; 
     } 
    } 
    } 
    dest.Close(); 
} 
+0

乍一看,在我看來,由於您正在閱讀具有不同數據的兩個文件,因此您的行可能會不同步。如果文件不是很大,您可能需要將它們加載到列表中並進行更正式的比較。 – IamIC

+2

你的代碼中有一些亂七八糟的東西,如果你使用「使用」,你不需要調用dispose,爲什麼你不使用StreamWriter的「using」,爲什麼使用FileStream並且什麼也不做呢...... – Giedrius

+0

順便說一句,'using'語句的全部內容,它會爲你調用Dispose,所以在'using'塊中調用Dispose,最好不需要。 – SWeko

回答

0
private void compare() 
{ 
    string FileName1 = @"c:\snapshot\1.txt"; 
    string FileName2 = @"c:\snapshot\2.txt"; 
    string FileNameOutput = @"c:\snapshot\log.txt"; //dir ??? 
    int counter = 0; // um what's this for you aren't using it. 

    using (FileStream fso = new FileStream(FileNameOutput, FileMode.Create, FileAccess.Write)) 
    { 
    TextWriter dest = new StreamWriter(fso); 
    using(FileStream fs1 = new FileStream(FileName1, FileMode.Open, FileAccess.Read)) 
    { 
     using (FileStream fs2 = new FileStream(FileName2, FileMode.Open, FileAccess.Read)) 
     { 
     TextReader firstFile = new StreamReader(fs1); 
     TextReader secondFile = new StreamReader(fs2); 
     while (((line1 = firstFile.ReadLine()) != null & (line2 = secondFile.ReadLine()) != null)) 
     { 
      if ((line1.Contains("*") || (!line1.Contains(line2))) 
      { 
      dest.Write(line2); // Writeline would give you an extra line? 
      } 
      counter++; // 
     } 
     } 
    } 
    fso.Flush(); 
} 

我讚揚的重載FileStream給你。按照我的方式進行操作,如果運行該實例的用戶沒有獲得所需的所有權限,則代碼將在實例化流時崩潰。這是展示你的意圖的一種很好的方式,以及你不知道的。

PS你知道包含的是案件和文化敏感嗎?

+0

我認爲你不需要手動沖洗:http://stackoverflow.com/questions/7710661/do-you-need-to-call-flush-on-a-stream-if-you-use-using-statement – Giedrius

+0

嗯,我知道它說你不應該但我見過它通過皮帶和支架來解決所有問題。我不相信這會發生,所以我現在稱之爲自衛。 –

1

一切都應該被寫入只要您點擊StreamReader上的關閉語句即可。如果你缺少東西,那麼可能是因爲某種原因你沒有到達那條線(即你崩潰)。另外,如果您正在編寫文件時(即程序仍在運行時),您不一定會看到所有內容(因爲它尚未關閉)。

通常,最好在StreamReader中使用using語句。這應該確保它總是被關閉。

+0

什麼是加載兩個文本文件到列表然後比較列表的最佳方式? –

+0

注意,如果您已經有一個使用輪到StreamReader的輸入,例如一個Filestream,然後把一個放在StreamReader上,當FileStream實例超出範圍時,它會給你一個已經處理好的錯誤。 –

0

不知道如果我理解你的邏輯比較正確的,但只要我分開全碼的比較,您可以在調整自己的需要:

public static void WriteDifferences(string sourcePath, string destinationPath, string differencesPath) 
    { 
     var sourceLines = File.ReadAllLines(sourcePath).ToList(); 
     var destinationLines = File.ReadAllLines(destinationPath).ToList();    

     // make lists equal size 
     if (sourceLines.Count > destinationLines.Count) 
     { 
      destinationLines.AddRange(Enumerable.Range(0, sourceLines.Count - destinationLines.Count).Select(x => (string)null)); 
     } 
     else 
     { 
      sourceLines.AddRange(Enumerable.Range(0, destinationLines.Count - sourceLines.Count).Select(x => (string)null)); 
     } 

     var differences = sourceLines.Zip(destinationLines, (source, destination) => Compare(source, destination)); 

     File.WriteAllLines(differencesPath, differences.Where(x => x != null)); 
    } 

    private static string Compare(string source, string destination) 
    { 
     return !source.Contains(destination) || source.Contains("*") ? destination : null; 
    } 
+0

不確定我瘋了你的假設,這是有足夠的內存在無所事事,以保持兩個文件和差異... –

+1

這取決於。如果我確定,那些文件不會超過1mb,我寧願輕易維護的代碼,如果不是這樣的話 - 確定,通過逐行閱讀和比較,這可能會比當前的方法慢(不知道如何緩存會逐行讀出)。 – Giedrius

+0

如果我知道文件很小,我沒有問題。看到,因爲我們不知道,只是認爲我會提到的問題,對於那些不喜歡間歇性的OOMs是他們的代碼:( –