2016-06-21 51 views
0

此問題是過去討論的延續HERE。所以,現在我設法讀取我的文本文件中的每一行,並閱讀特定列中的確切字符串。我現在的問題是,我想用另一個字符串值修改標籤基文件的Column(4)中的文本值。 例如,原始文件是這樣的:修改/替換C#中基於標籤的文本文件中的文本

ID1 25 800 Low 
ID2 25 700 Low 
ID3 25 600 Low 

我想更改爲:

ID1 25 800 High 
ID2 25 700 High 
ID3 25 600 High 

...這裏是我的全部代碼。我感謝您的幫助。

string route = @"C:\MyFile.txt"; 
FileStream fileStream2 = new FileStream(route, FileMode.Open); 

var m_readFile2 = new StreamReader(fileStream2); 
var m_writeFile2 = new StreamWriter(fileStream2); 

     string[] colArr1 = new string[100]; 
     string[] colArr2 = new string[100]; 
     string[] colArr3 = new string[100]; 
     string[] colArr4 = new string[100]; 
     int arrcount = 1; 

     while ((line = m_readFile2.ReadLine()) != null) 

     { 

      string col1 = ""; 
      string col2 = ""; 
      string col3 = ""; 
      string col4 = ""; 

       col1 = line.Split('\t')[0]; 
       col2 = line.Split('\t')[1]; 
       col3 = line.Split('\t')[2]; 
       col4 = line.Split('\t')[3]; 

       colArr1[arrcount] = col1; 
       colArr2[arrcount] = col2; 
       colArr3[arrcount] = col3; 
       colArr4[arrcount] = col4;  


       m_writeFile2.WriteLine("Serv" + arrcount + "\t" + "25" + "\t" + "400" + "\t" + "High"); 
       arrcount = arrcount + 1; 

回答

1

我建議你分割線到一個數組中,並把新行一起回來:

 string source = @"D:\MyFile.txt"; 
     string destination = @"D:\MyFile2.txt"; 

     int columnToChange = 3; 
     string newValueForColumn = "High"; 

     using (FileStream sourceStream = new FileStream(source, FileMode.Open)) 
     { 
      using (FileStream destinationStream = new FileStream(destination, FileMode.CreateNew)) 
      { 

       using (StreamReader sourceReader = new StreamReader(sourceStream)) 
       { 

        using (StreamWriter destinationWriter = new StreamWriter(destinationStream)) 
        { 
         string oldLine = string.Empty; 
         while ((oldLine = sourceReader.ReadLine()) != null) 
         { 
          string[] values = oldLine.Split('\t'); 
          StringBuilder newLine = new StringBuilder(); 
          if (values.Length > columnToChange) 
          { 
           values[columnToChange] = newValueForColumn; 
           for (int i = 0; i < values.Length; i++) 
           { 
            newLine.Append(values[i]); 
            if (i + 1 < values.Length) 
            { 
             newLine.Append('\t'); 
            } 
           } 
          } 
          else 
          { 
           newLine.Append(oldLine); 
          } 

          destinationWriter.WriteLine(newLine.ToString()); 
         } 
        } 
       } 
      } 
     } 

     // File.Delete(source); 
     File.Move(source, source + ".bak"); 
     File.Move(destination, source); 
    } 
+0

'stringbuilder'是追加字符串 – Eldho

+0

我最好的選擇已經在數組字符串中構建了完整的新行。這是一個簡單的文本文件。然而,當我使用的WriteLine,文本文件看起來像這樣,不修改,但增加了額外的線路: ServID1 25 800低 ServID2 25 700低 ServID3 25 600 LowServID1 25 800高 ServID2 25 700高 但我想它會修改文本而不會添加額外的行:-( – zakim

+0

真實且可以理解當你寫回到同一個文件時,你應該把所有newLine寫入一個新文件,當完成時你應該關閉這兩個文件,刪除舊文件,重新命名爲舊名 –

1

KISS

string text = File.ReadAllText(route); 

text = text.Replace("Low", "High"); 

File.WriteAllText(route, text); 
+0

我擔心這並不是滿足所有需求。如果不同的列也包含「低」,會怎麼樣?這不應該被取代。我認爲你的方法是「簡單和愚蠢」;-) –

+1

@ThomasVoß - 一切都可以......但是,提供的代碼完全符合要求。如果需求是其他 - 將會有另一個代碼。 [YAGNI](https://en.wikipedia.org/wiki/You_aren%27t_gonna_need_it)。 –

+0

謝謝你提醒我基本原則。你是完全正確的。所以現在有兩種需求的解決方案... –