两文本的key值比较
1 import java.io.BufferedReader; 2 import java.io.FileReader; 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.util.Hashtable; 6 7 /** 8 * 利用哈希表进行key值比较 9 * @author taojx 10 * 11 */ 12 public class HashTableComparator { 13 14 public static long RSHash(String key){ 15 long HashCode = 0; 16 int a = 63689; 17 int b = 378551; 18 for(int i=0;i<key.length();i++){ 19 HashCode = HashCode*a+key.charAt(i); 20 a = a*b; 21 } 22 return HashCode; 23 } 24 25 /** 26 * 判断冗余行(被注释行或空行) 27 * @param str 28 * @return 29 */ 30 public static boolean JudgeRedundancyStr(String str){ 31 str = str.replace(" ", ""); 32 if(str.indexOf("###") != -1 || str.length() == 0) 33 return true; 34 else 35 return false; 36 } 37 38 /** 39 * 将文本中的key值全部保存在hashtable中 40 * @param PathOfText 41 * @return 42 * @thkeys IOException 43 */ 44 public static Hashtable<Long, String> getKeytable(String PathOfText) throws IOException{ 45 46 long key; 47 String str; 48 String[] substr; 49 Hashtable<Long, String> keytable = new Hashtable<Long, String>(); 50 FileReader fr = new FileReader(PathOfText); 51 BufferedReader br = new BufferedReader(fr); 52 53 while((str=br.readLine())!=null){ 54 55 if(JudgeRedundancyStr(str)) 56 continue; 57 58 substr = str.split("="); 59 key = RSHash(substr[0]); 60 61 while(keytable.get(key) != null && keytable.get(key) != substr[0]) //查看该key值是否已经被占用,若已经被占用,则key++ 62 { 63 key++; 64 } 65 keytable.put(key, substr[0]); 66 } 67 68 fr.close(); 69 br.close(); 70 return keytable; 71 } 72 73 /** 74 * 判断该key值在该哈希表中是否存在 75 * @param key 76 * @param Keytable 77 * @return 78 */ 79 public static boolean ExistInKeytable(String key, Hashtable<Long, String> Keytable){ 80 long KeyOfKey; 81 boolean ExistOfKey = false; 82 boolean ExistOfValue = false; 83 String value; 84 85 KeyOfKey = RSHash(key); 86 87 while((value = Keytable.get(KeyOfKey)) != null) 88 { 89 if(value.equals(key)) 90 { 91 ExistOfValue = true; 92 break; 93 } 94 KeyOfKey++; 95 } 96 97 if( ExistOfValue == true) 98 ExistOfKey = true; 99 100 return ExistOfKey; 101 } 102 103 /** 104 * 将text1的内容与哈希表中text2中的内容比较,输出text1中hashtable没有的内容 105 * @param PathOfText1 106 * @param PathOfText2 107 * @param PathOfDiff 108 * @thkeys IOException 109 */ 110 public static void TextCompare(String PathOfText1, String PathOfText2, String PathOfDiff) throws IOException{ 111 112 String str; 113 String[] substr; 114 115 FileReader fr = new FileReader(PathOfText1); 116 BufferedReader br = new BufferedReader(fr); 117 PrintWriter pw = new PrintWriter(PathOfDiff); 118 Hashtable<Long, String> Keytable = new Hashtable<Long, String>(); 119 120 Keytable = getKeytable(PathOfText2); 121 122 while((str=br.readLine())!=null){ 123 124 if(JudgeRedundancyStr(str)) 125 continue; 126 127 substr = str.split("="); 128 129 if(!ExistInKeytable(substr[0], Keytable)) 130 { 131 pw.println(substr[0]); 132 pw.println(); 133 } 134 135 System.out.println("........Running........"); 136 } 137 138 System.out.println("........Finished........"); 139 fr.close(); 140 br.close(); 141 pw.close(); 142 } 143 144 public static void main(String[] args) throws IOException{ 145 146 String path1 = "D:\\compare\\Language-cn.txt"; 147 String path2 = "D:\\compare\\Language-zh_CN.txt"; 148 String path3 = "D:\\compare\\resultII\\KeyDiff-cn.txt"; 149 String path4 = "D:\\compare\\resultII\\KeyDiff-zh_CN.txt"; 150 151 TextCompare(path1, path2, path3); 152 TextCompare(path2, path1, path4); 153 154 } 155 }