2010-11-23 206 views
1

我有兩個文本文件bala.txtbala1.txt比較兩個文本文件

bala.txt包含文本行線

balamurugan,rajendran,chendurpandian 
christopher 
updateba 

bala1.txt包含文本行線

ba 

在這裏,我需要檢查bala1.txt與bala.txt並寫入日誌文件爲

LineNo : 0 : balamurugan,rajendran,chendurpandian 
LineNo : 2 : updateba 

現在它的寫作只有一條線作爲

LineNo : 0 : balamurugan,rajendran,chendurpandian 

,雖然循環後失控

這裏是我的代碼

while (((line = file.ReadLine()) != null & (line2 = file2.ReadLine()) != null)) 
         { 
          if (line.Contains(line2)) 
          { 
           dest.WriteLine("LineNo : " + counter.ToString() + " : " + line + "<br />"); 
          } 
          counter++; 
         } 

任何建議?

編輯:

string FilePath = txtBoxInput.Text; 
    string Filepath2 = TextBox1.Text; 
    int counter = 0; 
    string line; 
    string line2; 


     DirectoryInfo Folder = new DirectoryInfo(textboxPath.Text); 
     var dir = @"D:\New folder\log"; 

     if (Folder.Exists) 
     { 
      if (!Directory.Exists(dir)) 
       Directory.CreateDirectory(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)) 
      using (var dest = File.AppendText(Path.Combine(dir, "log.txt"))) 
      { 
       while (((line = file.ReadLine()) != null & (line2 = file2.ReadLine()) != null)) 
       { 
        if (line.Contains(line2)) 
        { 
         dest.WriteLine("LineNo : " + counter.ToString() + " : " + line + "<br />"); 
        } 

        counter++; 
       } 

      } 

}

EDIT(2): 我需要創建兩個文本文件到文件夾中的日誌,並與內容

LineNo : 0 : balamurugan,rajendran,chendurpandian 
LineNo : 2 : updateba 
寫入文本文件作爲 ba.txt

and ra.txt with contents as

LineNo : 0 : balamurugan,rajendran,chendurpandian 

任何建議?

EDIT(3): 我需要通過代碼,並在日誌文件夾中創建名爲Log文件夾ba.txtra.txt必須創建。

+0

你能粘貼完整的代碼嗎? – Singleton 2010-11-23 05:42:44

+0

@Hansmukh查看我的編輯 – bala3569 2010-11-23 05:46:36

回答

1

試試這個:

string FilePath = txtBoxInput.Text, Filepath2 = TextBox1.Text; 
int counter = 0; 
string line, line2; 

DirectoryInfo Folder = new DirectoryInfo(textboxPath.Text); 
var dir = @"D:\New folder\log"; 

if (Folder.Exists) 
    if (!Directory.Exists(dir)) Directory.CreateDirectory(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((line2 = file2.ReadLine()) != null) 
      { 
       //YOU NEED TO CHECK IF FILE ALREADY EXISTS 
       // AND YOU WANT TO OVERWRITE OR CREATE NEW 
       //WITH SOME OTHER NAME 
       //---------------------------------------CREATE NEW FILE FOR 
       //---------------------------------------EACH LINE IN file2 
       using (var dest = File.AppendText(Path.Combine(dir, line2 + ".txt"))) 
       { 
        while ((line = file.ReadLine()) != null) 
        { 
         if (line.Contains(line2)) 
          dest.WriteLine("LineNo : " + 
           counter.ToString() + " : " + line + "<br />"); 
         counter++; 
        } 
        //IF THE SECOND FILE ONLY CONTAINS 1 LINE THEN YOU 
        //DON'T NEED THIS. 
        //we need to go to begning of first file 
        file.BaseStream.Seek(0, SeekOrigin.Begin); 
        counter = 0; 
       } 
      } 
     } 
    } 
} 

編輯:從用戶那裏獲取文件的路徑。

如果要獲取目錄名稱來保存日誌文件,請給出一個按鈕以打開文件對話框以選擇文件或文件夾瀏覽器對話框。

//OPEN FILE -- you will need two buttons one 
//for each text boxes 
void btnFile_Click(object sender, EventArgs e) 
{ 
    var fbd = new OpenFileDialog(); 
    fbd.Multiselect = false; 
    fbd.CheckFileExists = true; 
    fbd.CheckPathExists = true; 
    if(fbd.ShowDialog()==DialogResult.Ok) 
    { 
     textBox1.Text = fbd.FileName; 
    } 
} 

//SELECT FOLDER 
string _logFolderPath;//use this inplace of @"D:\new folder\log"; 
void btnFolder_click(object sender, EventArgs e) 
{ 
    var fd = new FolderBrowserDialog(); 
    if(fd.ShowDialog()==DialogResult.OK) 
    { 
     _logFolderPath = fd.SelectedPath; 
    } 
} 
1

第二個文件讀取器的基礎流到達流的末尾,並且在下一次循環迭代之前未被重置。在比較之前,您需要將每個文件的所有行復制到內存中。試試這個:

List<string> file1Lines = new List<string>(); 
List<string> file2Lines = new List<string>(); 

while ((line = file.ReadLine()) != null) 
{ 
    file1Lines.Add(line); 
} 

while ((line2 = file2.ReadLine()) != null) 
{ 
    file2Lines.Add(line2); 
} 

foreach (string f1line in file1Lines) 
{ 
    foreach (string f2line in file2Lines) 
    { 
     if (f1line.Contains(f2line)) 
     { 
      dest.WriteLine("LineNo : " + counter.ToString() + " : " + f1line + "<br />"); 
     } 

    } 
    counter++; 
} 
+0

@Kyle Trauberman沒有其仍然只寫第一行 – bala3569 2010-11-23 05:46:14

+0

@Kyle Trauberman line2正在讀取下一行並返回null,因此while循環退出 – bala3569 2010-11-23 05:56:03

1
private void Comparer(string fileLocation1, string fileLocation2, string resultLocation) 
{ 
    StreamReader source = new StreamReader(fileLocation1); 
    StreamReader pattern = new StreamReader(fileLocation2); 
    StreamWriter result = File.CreateText(resultLocation); 

    //reading patterns 
    List<String> T = new List<string>(); 
    string line; 
    while ((line = pattern.ReadLine()) != null) 
     T.Add(line); 
    pattern.Close(); 

    //finding matches and write them in output 
    int counter = 0; 
    while ((line = source.ReadLine()) != null) 
    { 
     foreach (string pat in T) 
     { 
      if (line.Contains(pat)) 
      { 
       result.WriteLine("LineNo : " + counter.ToString() + " : " + line); 
       break; //just if you want distinct output 
      } 
     } 
     counter++; 
    } 

    source.Close(); 
    result.Close(); 
} 

----------------------------編輯----------- ----------------------- 爲你在評論中提到的一個

private void Comparer(string fileLocation1, string fileLocation2, string resultFolder) 
{ 
    StreamReader source = new StreamReader(fileLocation1); 
    StreamReader pattern = new StreamReader(fileLocation2); 
    Directory.CreateDirectory(resultFolder); 
    //reading patterns 
    List<String> T = new List<string>(); 
    string line; 
    while ((line = pattern.ReadLine()) != null) 
     T.Add(line); 
    pattern.Close(); 

    //finding matches and write them in output 
    int counter; 
    foreach (string pat in T) 
    { 
     StreamWriter result = File.CreateText(Path.Combine(resultFolder, pat + ".txt")); 
     source.BaseStream.Position = counter = 0; 
     while ((line = source.ReadLine()) != null) 
     { 
      if (line.Contains(pat)) 
       result.WriteLine("LineNo : " + counter.ToString() + " : " + line); 
      counter++; 
     } 
     result.Close(); 
    } 
    source.Close(); 
}