import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
/**
* {@docRoot}
*
Java版词典测试版,可以在控制台下输入要查询的单词,回车后会给出单词在词典中的释义
* 词典采用星际译王的词典,本程序主要针对英汉词典
*
* @author menglongbor
* @updateDate 2012-12-31
* @version v1.0.0
*
* 相关参考链接:
* http://blog.chinaunix.net/uid-20454005-id-1675913.
html
* http://hi.baidu.com/sean_zhu_xiang/item/1581342f88be430e73863eee
* http://blog.csdn.net/ranxiedao/article/details/7787342
* http://www.stardict.cn/
* http://www.huzheng.org/
* http://code.google.com/p/stardict-3/downloads/list
*
*/
public class testdict
{
final static intMAX_WORD= 256;// 最长输入单词字符数
final static intMAX_KEYS= 27;// 26个字母+"-"开头的后缀
final static intSIZEINT= 4;
final static StringKEY[]= {// 26个字母索引+"-"开头的后缀,不区分大小写
"A", "b", "c", "d", "e", "f",
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
"t", "u", "v", "w", "x", "y", "z", "-" };
public static InputStreamisidx= null;// 读取idx文件时所要的流
public static InputStreamisdict= null;// 读取dict文件时所要的流
public static longSTREAM_LOCAL= 0;// 记录单词索引在文件流中的位置
public static StringidxfileString= "oxford-gb.idx";// idx文件路径
public static StringdictfileString= "oxford-gb.dict";// dict文件路径
/**
* 从idx文件中获取当前目标单词
* @param word_buf 保存的是c/c++字符串数组转换为JAVA字符串
* @param data_poffset 用来保存单词的data偏移位置信息
* @param data_plength 用来保存单词的data长度信息
* @param len
* @return
*/
public static boolean get_word(String[] word_buf, int[] data_poffset,
int[] data_plength, int[] len)
{
// int len = 0;
boolean flag = true;
len[0] = 0;
int index = -1;
byte wd[] = new byte[MAX_WORD];
int value = 0;
try
{
// 读取单词,对每个字母开头的单词都进行搜索,最多考虑256个字符的单词,
// 读到单词结束符\0时赋值表达式的值就不满足while条件而退出
while (true)
{
index = isidx.read();
STREAM_LOCAL++;// 每读取一次,位置标识加一以记录下单词在文件流中的起始位置
if (index == -1)
{
// isidx.reset();
flag = false;
break;
}
if ((index != 0) && (len[0] < MAX_WORD))
{
wd[len[0]] = (byte) index;// 将int转换为byte
len[0]++;
} else
{
break;
}
}
// 转换为JAVA字符串
// 此处不用再需要像c/c++那样去掉了最后那个结束符了
byte wd2[] = new byte[len[0]];
for (int i = 0; i < len[0]; i++)
{
wd2[i] = wd[i];
}
word_buf[0] = new String(wd2);
// System.out.println("get_word:"+word_buf[0]+" len:"+len[0]);
// wd = null;// 释放内存
// wd2 = null;
// 读取偏移量值
for (int i = 0; i < SIZEINT; i++)
{
// 将4个byte转换为int
int shift = (4 - 1 - i) * 8;
index = isidx.read();
STREAM_LOCAL++;// 每读取一次,位置标识加一以记录下单词在文件流中的起始位置
if (index == -1)
{
// isidx.reset();
flag = false;
return flag;
}
value += (index & 0x00FF) << shift;
}
data_poffset[0] = value;
// 读取区块大小值
value = 0;
for (int i = 0; i < SIZEINT; i++)
{
// 将4个byte转换为int
int shift = (4 - 1 - i) * 8;
index = isidx.read();
STREAM_LOCAL++;// 每读取一次,位置标识加一以记录下单词在文件流中的起始位置
if (index == -1)
{
// isidx.reset();
flag = false;
return flag;
}
value += (index & 0x00FF) << shift;
}
data_plength[0] = value;
}
catch (Exception e)
{
System.out.println("idx file read error!");
}
// System.out.println("Now local is:"+STREAM_LOCAL);
// 得