获取GBK编码的汉字笔画数

来源:互联网 发布:mac远程光盘怎么用 编辑:程序博客网 时间:2024/04/28 04:37

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