请写一个程序找出附件中文件重复的行
输出:
输出一个文本文件,给出哪些行是重复的,第一次出现的行号,格式如下
行号 此行的文本内容
并给出此程序的运行时间
我大概写了一下,代码如下:
static void Main(string[] args)
{
TimeSpan ts1 = new TimeSpan(DateTime.Now.Ticks); //get current ticks.
StreamReader sr = new StreamReader(@"D:\文本.txt", System.Text.Encoding.Default); //the path of source file.
String strline="";
int number=1;//indicate the line number.
StringBuilder sb=new StringBuilder();
Hashtable myHT=new Hashtable ();
while((strline=sr.ReadLine())!=null)
{
if (myHT.ContainsKey(strline))
{
sb.Append(myHT[strline].ToString() + " " + strline + "\r\n");
}
else
{
myHT.Add(strline, number);
}
number++;
}
sr.Close();
StreamWriter wr = new StreamWriter(@"D:\1.txt");//the output file.
wr.Write(sb+"");
wr.Close();
TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks); //get current ticks.
string spanTotalSeconds = ts2.Subtract(ts1).Duration().TotalSeconds.ToString(); //seconds
Console.WriteLine(spanTotalSeconds);
Console.ReadKey();
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-539840/ ,如需转载,请注明出处,否则将追究法律责任。