CompareFile

来源:互联网 发布:破云扇淘宝 编辑:程序博客网 时间:2024/06/06 04:30

package Stream;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class CompareFile {

 public static void main(String args[]) {

  Set fSet1 = new HashSet();
  Set fSet2 = new HashSet();
  try {
   readFileToSet("f://comp1.txt", fSet1);
   readFileToSet("f://comp2.txt", fSet2);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  compareTwoSets(fSet1, fSet2);

 }

 public static void readFileToSet(String fileName1, Set fSet1)
   throws IOException {

  BufferedReader reader1 = null;
  try {
   reader1 = new BufferedReader(new FileReader(fileName1));
   String sTemp1;
   while ((sTemp1 = reader1.readLine()) != null) {
    if (!sTemp1.trim().equals(""))
     fSet1.add(sTemp1);
   }
   reader1.close();
  } catch (FileNotFoundException e) {

   e.printStackTrace();
  } catch (IOException e) {

   e.printStackTrace();

  } finally {

   reader1.close();
  }

 }

 public static void compareTwoSets(Set fSet1, Set fSet2) {
  Iterator it1 = fSet1.iterator();
  String st = null;
  while (it1.hasNext()) {
   st = (String) it1.next();
   if (!fSet2.contains(st))
    System.out.println(st);
  }

 }

 public static void getAndSplitFileToSet(String fileName1, Set fSet1)
   throws IOException {

  BufferedReader reader1 = null;
  try {
   reader1 = new BufferedReader(new FileReader(fileName1));
   String sTemp1;
   while ((sTemp1 = reader1.readLine()) != null) {
    if (!sTemp1.trim().equals("")) {
     sTemp1 = filter(sTemp1.trim());
     if (sTemp1 != null)
      fSet1.add(sTemp1);

    }

   }
   reader1.close();
  } catch (FileNotFoundException e) {

   e.printStackTrace();
  } catch (IOException e) {

   e.printStackTrace();

  } finally {

   reader1.close();
  }

 }

 public static String filter(String sTemp)
 {
  
  String[] sT=sTemp.split(",");
  String sR=null;
  if (sT[0].equals(""))
   if (sT[0].equals(""))
    if (sT[0].equals(""))
                sR=sT[1];
  
  
  return sR;
  
  
  
 }
}

原创粉丝点击