1:首先把Excel中的文本复制到txt中,复制如下:
A表:
证件号 工号 姓名
310110xxxx220130004 101 傅家宜
310110xxxx220130005 102 顾音琪
310110xxxx220130006 103 郭加峤
310110xxxx220130007 104 胡奕蕾
310110xxxx220130010 105 凌家蔚
310110xxxx220130011 106 卢彦菁
B表:
证件号 工号 姓名
310110xxxx220130004 111 傅家宜
310110xxxx220130005 102 顾音琪
310110xxxx220130006 103 郭嘉峤
310110xxxx220130007 104 胡奕蕾
310110xxxx220130010 105 凌家蔚
310110xxxx220130012 107 潘家莹
2:代码和运行结果如下:
1 package aa; 2 import java.io.BufferedReader; 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.InputStreamReader; 6 import java.util.Hashtable; 7 import java.util.Map.Entry; 8 9 public class DuplicateItem { 10 public static Hashtable<String, String> readTxtFile(String filePath){ 11 Hashtable<String,String> table =new Hashtable<String, String>(); 12 try { 13 String encoding="GBK"; 14 File file=new File(filePath); 15 if(file.isFile() && file.exists()){ 16 InputStreamReader read = new InputStreamReader( 17 new FileInputStream(file),encoding); 18 BufferedReader bufferedReader = new BufferedReader(read); 19 String lineTxt = null; 20 while((lineTxt = bufferedReader.readLine()) != null){ 21 String key = lineTxt.substring(0, lineTxt.indexOf(" ")); 22 String value = lineTxt.substring(lineTxt.indexOf(" ")+1); 23 table.put(key.trim(), value.trim()); 24 } 25 read.close(); 26 }else{ 27 System.out.println("找不到指定的文件"); 28 } 29 } catch (Exception e) { 30 System.out.println("读取文件内容出错"); 31 e.printStackTrace(); 32 } 33 return table; 34 } 35 36 public static void printall(Hashtable<String,String> ht, Hashtable<String, String> ht2){ 37 for(Entry<String, String> en : ht.entrySet()){ 38 if(null == ht2.get(en.getKey())){ 39 System.out.println(" B中没有A中value为" + en.getValue().replace(" ", "") + "的项 "); 40 }else if(!en.getValue().equals(ht2.get(en.getKey()))){ 41 System.out.println("A中value为: " + en.getValue().replace(" ", "") + " 与B中 "+ ht2.get(en.getKey()).replace(" ", "") +" 不同"); 42 } 43 44 } 45 46 for(Entry<String, String> en2 : ht2.entrySet()){ 47 if(ht.get(en2.getKey()) == null){ 48 System.out.println(" A中没有B中value为" + en2.getValue().replace(" ", "") + "的项 "); 49 }else if(!en2.getValue().equals(ht.get(en2.getKey()))){ 50 System.out.println("B中value为: " + en2.getValue().replace(" ", "") + " 与A中 "+ ht.get(en2.getKey()).replace(" ", "") +" 不同"); 51 } 52 53 } 54 } 55 56 public static void main(String argv[]){ 57 Hashtable<String,String> table =new Hashtable<String, String>(); 58 Hashtable<String,String> table2 =new Hashtable<String, String>(); 59 String filePath = "C:\Users\Administrator\Desktop\c.txt"; 60 String filePath2 = "C:\Users\Administrator\Desktop\d.txt"; 61 table = readTxtFile(filePath); 62 table2 = readTxtFile(filePath2); 63 if(table != null && table2 != null){ 64 printall(table,table2); 65 } 66 67 } 68 }