这个问题是过去讨论的延续。因此,现在我设法读取了文本文件中的每一行,并读取特定列中的精确字符串。我现在的问题是,我希望用另一个字符串值修改Tab-Base文件的第(4)列中的文本值。例如,原始文件是这样的:
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;
亲吻
string text = File.ReadAllText(route);
text = text.Replace("Low", "High");
File.WriteAllText(route, text);
我建议您将行分为数组,然后将新线路放回原处:
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);
}
//Works fine Thomas Voß - I've just added a line to ensure that Column Header is
//also not changed
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)
{
if (values[columnToChange] != "ColumnHeaderName")
{
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);
}