在一個文本文件中比較每倏記錄輸出相同的行
比如說:
文件:
1
123
123456
456789
123456
789
0123
123
那麼輸出結果:
123
123456
請各位幫賜教!謝謝!

解决方案 »

  1.   

    string fileName = System.IO.Path.Combine(Application.StartupPath,"1.txt");
    string tmp = null;
    Hashtable ht = new Hashtable();
    using (System.IO.StreamReader sr = new System.IO.StreamReader(fileName)) 
    {
    while ((tmp = sr.ReadLine()) != null)
    if (tmp.Trim().Length != 0)
    if (ht.Contains(tmp)) 
    sw.WriteLine(tmp);
    else
    ht[tmp] = 0;
    }
      

  2.   

    ht[tmp] = 0; 请问这个是什么意思啊!
      

  3.   

    sw.WriteLine(tmp); 
    改成Consoleht[tmp] = 0; 主要是用hashtable的键,随便赋了个0
      

  4.   

    Console.WriteLine(tmp); 
    是控制台输出winform程序,可以用MessageBox.Show弹出来看看,
    或者写到一个文本里面,看结果的方法多的很,看你自己需要
      

  5.   

    string fileName = System.IO.Path.Combine(Application.StartupPath,"1.txt"); 
    string str= null; 
     
    using (System.IO.StreamReader sr = new System.IO.StreamReader(fileName)) 

    while ((tmp = sr.ReadLine()) != null) 

       if (!tempList.Contains(str))
            {
                tempList.Add(str); //将不重复的权限数据录入到tempList
            }
    else

    输出 str


    }
      

  6.   

    ArrayList tempList = new ArrayList(); 
      

  7.   

    string fileName = System.IO.Path.Combine(Application.StartupPath,"1.txt"); 
    string str= null; 
    string strNew="";using (System.IO.StreamReader sr = new System.IO.StreamReader(fileName)) 

    while ((tmp = sr.ReadLine()) != null) 
    { 
      if (!tempList.Contains(str)) 
            { 
                tempList.Add(str); //将不重复的权限数据录入到tempList 
            } 
    else 
    { 
    strNew =strNew+str;
      } 
    } 
        FileStream fs = new FileStream("d:/2.txt", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312"));//通过指定字符编码方式可以实现对汉字的支持,否则在用记事本打开查看会出现乱码
            sw.Flush();
            sw.BaseStream.Seek(0, SeekOrigin.Begin);
            sw.Write(Environment.NewLine);
            sw.WriteLine(strNew);
            sw.Flush();
            sw.Close();
      

  8.   

    还是不行咯,相同的数据还是输不出来,是空白的! if (!tempList.Contains(str)) 
     { 
        tempList.Add(str); //将不重复的权限数据录入到tempList 
     } 
    else 
    { 
       strNew =strNew+str; 
     }
       
    else 好像都没落执行过呢!
    所以输了,出来的都是空值!
      

  9.   

    string fileName = System.IO.Path.Combine(Application.StartupPath,"1.txt");
    string tmp = null;
    Hashtable ht = new Hashtable();
    using (System.IO.StreamReader sr = new System.IO.StreamReader(fileName)) 
    {
    fileName = System.IO.Path.Combine(Application.StartupPath,"2.txt");
    System.IO.StreamWriter sw = new System.IO.StreamWriter(fileName);
    while ((tmp = sr.ReadLine()) != null)
    {
    if (tmp.Trim().Length != 0)
    if (ht.Contains(tmp)) 
    sw.WriteLine(tmp);
    else
    ht[tmp] = 0;
    }
    sw.Close();

    }
      

  10.   

    代码好用啊
    static void Main(string[] args)
            {
                string fileName ="c:/1.txt";
                string tmp = null;
                Hashtable ht = new Hashtable();
                using (System.IO.StreamReader sr = new System.IO.StreamReader(fileName))
                {
                    while ((tmp = sr.ReadLine()) != null)
                    {
                        if (tmp.Trim().Length != 0)
                            if (ht.Contains(tmp))
                            {                            //System.IO.StringWriter sw = new System.IO.StringWriter();
                                //sw.WriteLine(tmp);
                                System.Console.WriteLine(tmp);                        }
                            else
                            {
                                ht[tmp] = 0;
                            }
                    }
                    System.Console.ReadLine();
                }
            }