獲取GBK編碼的漢字筆畫數

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;

    }

}

 

使用該方法獲取1000000GBK漢字筆畫數的時間是:7079 ms

以下是測試結果:

= 4

= 11

= 11

= 5

= 13

= 9

= 12

= 7

= 4

= 15

Time: 7079

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章