📄 wordindex.java
字号:
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; // 关键词哈希表
static String[] FileList = { // 存放原始文件内容,数组每一个代表一个文件的内容
" 北 京 师 范 大 学",
" 北 师 大 附 属 实 验 小 学 ",
" 北 师 大 第 二 附 属 中 学 " };
public static void main(String[] args) throws IOException {
try {
index();
search("北");
} catch (Exception e) {
System.err.println("下载失败,请检查输入地址是否正确。");
System.exit(1);
}
}
//根据输入的内容进行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); // 根据关键词获取Hash表中的索引项列表
while( item != null ) // 循环显示索引项内容
{
System.out.println("search : File number :" + item.get_id()); // 显示编号
System.out.println("search : File offset :" + item.get_pos()); // 显示偏移
System.out.println("search : File Content :" + FileList[item.get_pos()]); // 显示内容
item = item.get_next(); // 获取下一个索引项
}
}catch (Exception e) {
throw e;
}
System.out.println("search : ------ end ------ ");
}
public static void index() throws Exception { // 根据给定的内容按照汉字建立Hash索引
infoItem item,item2;
System.out.println("index : ------ begin ------");
KeywordIdx = new Hashtable(); // 创建关键词Hash表
try {
System.out.println("index : Hash Table initial Size: " + KeywordIdx.size() ); // 关键词初始长度
for(int i =0 ;i < 3 ;i++) // 循环计算文本存放位置,添加到关键词Hash表
{
int len = FileList[i].length();
for( int j = 0; j < len; j++ )
{
item = new infoItem(i,i); // 生成文件内容存放位置的附属信息
String key = FileList[i].substring(j, j+1);
System.out.print(key );
if(!KeywordIdx.containsKey(key))
{
KeywordIdx.put(key , item); // 添加关键字索引到Hash表
} else {
item2 = (infoItem)KeywordIdx.get(key); // 提取原始的存储项
item.set_next(item2); // 新文件节点添加到链表头
KeywordIdx.put(key , item); // 添加关键字索引到Hash表
}
}
System.out.println("");
}
System.out.println("index : Hash Table finish Size: " + KeywordIdx.size() );
}catch (Exception e) {
throw e;
}
System.out.println("index : ------ end ------ ");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -