Chinaunix首页 | 论坛 | 博客
  • 博客访问: 264228
  • 博文数量: 757
  • 博客积分: 40040
  • 博客等级: 大将
  • 技术积分: 4935
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-09 12:37
文章分类

全部博文(757)

文章存档

2011年(1)

2008年(756)

我的朋友

分类:

2008-09-09 12:38:34

    一、读取Unicode文件


    /**

     * 读取Unicode编码文本文件

     * @param resource String - 文件名

     * @return String - Unicode文本

     */

    public static String read_Uni(String resource) {

        byte word_uni[] = new byte[1024];

        String strReturn = null;

        InputStream is;

        try {

            is = instance.getClass().getResourceAsStream(resource);

            is.skip(2);            // 跳过两个字节的文件头

            is.read(word_uni);

            is.close();

            StringBuffer stringbuffer = new StringBuffer("");

            for (int j = 0; j < word_uni.length; ) {

                int l = word_uni[j++];

                int h = word_uni[j++];

                char c = (char) ((l & 0xff) | ((h << 8) & 0xff00));

                stringbuffer.append(c);

            }

            strReturn = stringbuffer.toString();

        } catch (IOException ex) {

            System.out.println(ex);

        } finally {

            is = null;

        }

        return strReturn;

}

 

    二、读取UTF-8文件


    /**

     * 读取UTF-8编码文本文件

     * @param resource String - 文件名

     * @return String - UTF-8文本

     */

    public static String read_UTF(String resource) {

        byte word_utf[] = new byte[1024];

        String strReturn = null;

        InputStream is;

        try {

            is = instance.getClass().getResourceAsStream(resource);

            is.read(word_utf);

            is.close();

            strReturn = new String(word_utf, "UTF-8");

        } catch (IOException ex) {

            System.out.println(ex);

        }

        return strReturn;

   }

 

【责编:Youping】

--------------------next---------------------

阅读(117) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~