我有两个文本文件bala.txt
和bala1.txt
bala.txt
包含逐行文字
balamurugan,rajendran,chendurpandian
christopher
updateba
bala1.txt
包含逐行文字
ba
在这里,我需要使用bala1.txt
检查bala.txt
并将日志文件写入
Pattern Name:ba
LineNo : 1 : balamurugan,rajendran,chendurpandian
LineNo : 3 : updateba
现在写这样的
Pattern Name :ba
LineNo : 1 : balamurugan,rajendran,chendurpandian
LineNo : 2 : christopher
Pattern Name :ba
LineNo : 3 : updateba
这是我的代码
while ((line = file.ReadLine()) != null)
{
if (line.IndexOf(line2,StringComparison.CurrentCultureIgnoreCase) != -1)
dest.WriteLine("Pattern Name :" + line2);
dest.WriteLine("LineNo : " + counter.ToString() + " : " + line);
counter++;
}
file.BaseStream.Seek(0, SeekOrigin.Begin);
//(0, SeekOrigin.Begin);
counter = 1;
我不知道如何只写一次模式名称ba ...任何建议??
答案 0 :(得分:1)
要对代码进行最少量的更改,您可以这样更改:
bool patternwritten = false;
while ((line = file.ReadLine()) != null)
{
if (line.IndexOf(line2,StringComparison.CurrentCultureIgnoreCase) != -1)
if( !patternwritten){
dest.WriteLine("Pattern Name :" + line2);
patternwritten = true;
}
dest.WriteLine("LineNo : " + counter.ToString() + " : " + line);
counter++;
}
file.BaseStream.Seek(0, SeekOrigin.Begin);
//(0, SeekOrigin.Begin);
counter = 1;