?? wordindex.java
字號(hào):
package chapter2;
import java.io.*;
import java.net.*;
import java.util.*;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.LinkedList;
import java.util.Vector;
public class WordIndex {
static Hashtable KeywordIdx; // 關(guān)鍵詞哈希表
static String[] FileList = { // 存放原始文件內(nèi)容,數(shù)組每一個(gè)代表一個(gè)文件的內(nèi)容
" 北 京 師 范 大 學(xué)",
" 北 師 大 附 屬 實(shí) 驗(yàn) 小 學(xué) ",
" 北 師 大 第 二 附 屬 中 學(xué) " };
public static void main(String[] args) throws IOException {
try {
index();
search("北");
} catch (Exception e) {
System.err.println("下載失敗,請(qǐng)檢查輸入地址是否正確。");
System.exit(1);
}
}
//根據(jù)輸入的內(nèi)容進(jìn)行Hash檢索
public static void search(String keyword) throws Exception {
infoItem item;
System.out.println("search : ------ begin ------");
if( null ==KeywordIdx )
{
return;
}
try {
item = (infoItem )KeywordIdx.get(keyword); // 根據(jù)關(guān)鍵詞獲取Hash表中的索引項(xiàng)列表
while( item != null ) // 循環(huán)顯示索引項(xiàng)內(nèi)容
{
System.out.println("search : File number :" + item.get_id()); // 顯示編號(hào)
System.out.println("search : File offset :" + item.get_pos()); // 顯示偏移
System.out.println("search : File Content :" + FileList[item.get_pos()]); // 顯示內(nèi)容
item = item.get_next(); // 獲取下一個(gè)索引項(xiàng)
}
}catch (Exception e) {
throw e;
}
System.out.println("search : ------ end ------ ");
}
public static void index() throws Exception { // 根據(jù)給定的內(nèi)容按照漢字建立Hash索引
infoItem item,item2;
System.out.println("index : ------ begin ------");
KeywordIdx = new Hashtable(); // 創(chuàng)建關(guān)鍵詞Hash表
try {
System.out.println("index : Hash Table initial Size: " + KeywordIdx.size() ); // 關(guān)鍵詞初始長度
for(int i =0 ;i < 3 ;i++) // 循環(huán)計(jì)算文本存放位置,添加到關(guān)鍵詞Hash表
{
int len = FileList[i].length();
for( int j = 0; j < len; j++ )
{
item = new infoItem(i,i); // 生成文件內(nèi)容存放位置的附屬信息
String key = FileList[i].substring(j, j+1);
System.out.print(key );
if(!KeywordIdx.containsKey(key))
{
KeywordIdx.put(key , item); // 添加關(guān)鍵字索引到Hash表
} else {
item2 = (infoItem)KeywordIdx.get(key); // 提取原始的存儲(chǔ)項(xiàng)
item.set_next(item2); // 新文件節(jié)點(diǎn)添加到鏈表頭
KeywordIdx.put(key , item); // 添加關(guān)鍵字索引到Hash表
}
}
System.out.println("");
}
System.out.println("index : Hash Table finish Size: " + KeywordIdx.size() );
}catch (Exception e) {
throw e;
}
System.out.println("index : ------ end ------ ");
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -