一个demo,大概思路是这样:用Properties的load()来加载一个固定格式的文本文件注1,然后直接当Hashtable用。
package org.navyblue.tests;
import java.util.*;
/**//**
*
* 类似qq的汉字拼音首字查询方法
* by zqc 2005-08-04
*
***/
public class pyTest
{
public pyTest(){}
public static void main(String[] args)throws Exception{
Properties pyMap = new Properties();
//拼音隐射表
pyMap.load(new java.io.FileInputStream("pyutf.txt"));
//测试字符
String[] testStr = {"我们","阿哈","嘎嘎","干什么","郑晴川","谁"};
//模拟输入
String input = "g";
System.out.println ("输入查询字符:g");
Vector inis = new Vector();
Vector results = new Vector();
Vector results2 = new Vector();
//返回所有测试字符串的首字的打头拼音
for( int i =0 ; i < testStr.length ; i++ ){
String ini =new String( new char[] { testStr[i].charAt(0)} );
String k = (String)pyMap.get(ini);
results.add(k);
results2.add(testStr[i]);
}
//输出结果
int l = results.size();
for(int i = 0 ; i < l ; i++){
String c = (String)results.get(i);
if(c.equals(input)){
System.out.println (results2.get(i));
}
}
}
}
----
注1:这个映射文件系网上找来的,大约有14K的汉字量,一般也够用了~用文本工具对原先的格式做了些修改使它符合Properties的格式,然后再用native2ascii转成utf-8编码。
package org.navyblue.tests;
import java.util.*;
/**//**
*
* 类似qq的汉字拼音首字查询方法
* by zqc 2005-08-04
*
***/
public class pyTest
{
public pyTest(){}
public static void main(String[] args)throws Exception{
Properties pyMap = new Properties();
//拼音隐射表
pyMap.load(new java.io.FileInputStream("pyutf.txt"));
//测试字符
String[] testStr = {"我们","阿哈","嘎嘎","干什么","郑晴川","谁"};
//模拟输入
String input = "g";
System.out.println ("输入查询字符:g");
Vector inis = new Vector();
Vector results = new Vector();
Vector results2 = new Vector();
//返回所有测试字符串的首字的打头拼音
for( int i =0 ; i < testStr.length ; i++ ){
String ini =new String( new char[] { testStr[i].charAt(0)} );
String k = (String)pyMap.get(ini);
results.add(k);
results2.add(testStr[i]);
}
//输出结果
int l = results.size();
for(int i = 0 ; i < l ; i++){
String c = (String)results.get(i);
if(c.equals(input)){
System.out.println (results2.get(i));
}
}
}
}
----
注1:这个映射文件系网上找来的,大约有14K的汉字量,一般也够用了~用文本工具对原先的格式做了些修改使它符合Properties的格式,然后再用native2ascii转成utf-8编码。