获取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
代码比较简单,重点在配置文件中,由于文件的内容比较多没办法为大家展示出来,有需要的朋友可以留下自己邮箱,我给你们发到邮箱里。
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
相关文章推荐
- Java中获取GBK编码汉字的拼音字头方法。
- Java中获取GBK编码汉字的拼音首字母(包括生僻字)
- 如何获取汉字的首笔画编码
- 汉字编码(【Unicode】 【UTF-8】 【Unicode与UTF-8之间的转换】 【汉字 Unicode 编码范围】【中文标点Unicode码】【GBK编码】【批量获取汉字UNICODE码】)
- 汉字的奥秘: 获取汉字的笔画数
- 汉字为何不能用笔画编码--信息论系列
- 对GBK的理解(内附全部字符编码列表):扩充的2万汉字低字节的高位不等于1,而且还剩许多编码空间没有利用
- PHP中不同编码的汉字占的字节数不同gbk,GBK,UTF-8,utf-8
- PHP中不同编码的汉字占的字节数不同gbk,GBK,UTF-8,utf-8
- SQL根据GBK编码取汉字的拼音首字母的存储过程
- iOS GBK编码与汉字相互转换 BABA <---> 汉
- UTF-8,Unicode,GBK,希腊字母读法,ASCII码表,HTTP错误码,URL编码表,HTML特殊字符,汉字编码简明对照表
- C#_汉字与GBK,Unicode,UTF-8编码之间的转换(by daode1212)
- editplus编写java时遇到编码GBK的不可映射字符,即不能使用汉字的时候
- 利用java对基本汉字(unicode编码4E00-9FA5)进行笔画排序
- C语言 获取汉字unicode和utf-8编码
- 通过GBK编码提取中文汉字的拼音首字母
- C#中如何获取汉字的笔画数和汉字的拼音
- 获取字符串中所有汉字的笔画总和
- 汉字为何不能用笔画编码--信息论系列