1:首先把Excel中的文本复制到txt中,复制如下:
A表:
证件号 工号 姓名
310110xxxx220130004 101 傅家宜
310110xxxx220130005102顾音琪
310110xxxx220130006103郭加峤
310110xxxx220130007104胡奕蕾
310110xxxx220130010105凌家蔚
310110xxxx220130011106卢彦菁
B表:
证件号 工号 姓名
310110xxxx220130004 111 傅家宜
310110xxxx220130005102顾音琪
310110xxxx220130006103郭嘉峤
310110xxxx220130007104胡奕蕾
310110xxxx220130010105凌家蔚
310110xxxx220130012107潘家莹
2:代码和运行结果如下:
package aa;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Hashtable;
import java.util.Map.Entry; public class DuplicateItem {
public static Hashtable<String, String> readTxtFile(String filePath){
Hashtable<String,String> table =new Hashtable<String, String>();
try {
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
String key = lineTxt.substring(0, lineTxt.indexOf("\t"));
String value = lineTxt.substring(lineTxt.indexOf("\t")+1);
table.put(key.trim(), value.trim());
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
return table;
} public static void printall(Hashtable<String,String> ht, Hashtable<String, String> ht2){
for(Entry<String, String> en : ht.entrySet()){
if(null == ht2.get(en.getKey())){
System.out.println("\n\tB中没有A中value为" + en.getValue().replace("\t", "") + "的项\n");
}else if(!en.getValue().equals(ht2.get(en.getKey()))){
System.out.println("A中value为: " + en.getValue().replace("\t", "") + "\t与B中 "+ ht2.get(en.getKey()).replace("\t", "") +" 不同");
} } for(Entry<String, String> en2 : ht2.entrySet()){
if(ht.get(en2.getKey()) == null){
System.out.println("\n\tA中没有B中value为" + en2.getValue().replace("\t", "") + "的项\n");
}else if(!en2.getValue().equals(ht.get(en2.getKey()))){
System.out.println("B中value为: " + en2.getValue().replace("\t", "") + "\t与A中 "+ ht.get(en2.getKey()).replace("\t", "") +" 不同");
} }
} public static void main(String argv[]){
Hashtable<String,String> table =new Hashtable<String, String>();
Hashtable<String,String> table2 =new Hashtable<String, String>();
String filePath = "C:\\Users\\Administrator\\Desktop\\c.txt";
String filePath2 = "C:\\Users\\Administrator\\Desktop\\d.txt";
table = readTxtFile(filePath);
table2 = readTxtFile(filePath2);
if(table != null && table2 != null){
printall(table,table2);
} }
}