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