您的位置:首页 > 其它

获取GBK编码的汉字笔画数

2008-01-08 18:21 585 查看
GBK编码的汉字大概20000个左右,简繁两种字体的笔画数都可以得到,以下是源代码;
代码比较简单,重点在配置文件中,由于文件的内容比较多没办法为大家展示出来,有需要的朋友可以留下自己邮箱,我给你们发到邮箱里。

Chinese.java
/**
* 获取GBK编码的汉字笔画数
*
* @author 李赵伟 Create: 10:58:25 AM Dec 22, 2007
*/
public class Chinese {

/**
* 测试
*/
public static void main(String[] args) {
test();
}

static void test() {
long s = System.currentTimeMillis();
// String cn = "中国推出新型单兵火箭";
String cn = "中國推出新型單兵火箭";
char c = '國';
for (int i = 0; i < cn.length(); i++) {
c = cn.charAt(i);
Out.pln(c + " = " + countStroke(c));
}

final int SIZE = 1000000;
for (int j = 0; j < SIZE; j++) {
for (int i = 0; i < cn.length(); i++) {
c = cn.charAt(i);
countStroke(c);
}
}
long e = System.currentTimeMillis() - s;
Out.pln("Time: " + e);
}

/**
* 获取汉字的笔画数
*
* @param cn
* 一个汉字
* @return 汉字的笔画数
*/
public static int countStroke(char cn) {
int index = ((int) cn) - 0x4E00;
Integer r = GBKStrokeTable.gbkStrokeTable().get(index);
if (null == r)
return 0;
else
return r.intValue();
}
}

GBKStrokeTable.java
/**
* GBK编码汉字笔画数列表
*
* @author 李赵伟 Create: 3:30:41 PM Dec 24, 2007
*/
public class GBKStrokeTable {
private static List<Integer> gbkStrokeTable;
private String strokes;

private GBKStrokeTable() {
init();
}

private void init() {
InputStream is = getClass().getResourceAsStream("gbkstroke.properties");
Properties p = new Properties();
try {
try {
p.load(is);
} finally {
if (null != is)
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}

strokes = Stringutils.toGBKString(p.getProperty("gbkstroke"),
Stringutils.GBK);
String[] a = strokes.split(",");
gbkStrokeTable = new ArrayList<Integer>();
for (int i = 0; i < a.length; i++) {
gbkStrokeTable.add(Integer.valueOf(a[i]));
}
}

/**
* @return 获得GBK编码的汉字笔画数列表
*/
public static List<Integer> gbkStrokeTable() {
if (null != gbkStrokeTable)
return gbkStrokeTable;

new GBKStrokeTable();
return gbkStrokeTable;
}
}

使用该方法获取1000000个GBK汉字笔画数的时间是:7079 ms
以下是测试结果:
中 = 4
國 = 11
推 = 11
出 = 5
新 = 13
型 = 9
單 = 12
兵 = 7
火 = 4
箭 = 15
Time: 7079
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: