不管你曾经怎样,但请从现在开始做一个正直的人,男人要有责任心。
首页 arrow 开发技术 arrow 程序设计 arrow J2ME中读取Unicode和UTF-8编码文件

J2ME中读取Unicode和UTF-8编码文件 输出PDF 打印 E-mail
Author Author: 一滴蔚蓝色 | Date Date: 2008-06-09 | View Count View: 319 | section & Category 开发技术 -  程序设计

    一、读取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;

   }



收藏到您的网摘: Google书签 Yahoo书签 雅虎收藏夹 365Key网摘 新浪ViVi 百度收藏 天极网摘 diglog 和讯网摘 POCO网摘 YouNote网摘 博拉网 天下图摘 spurl blogmarks BlinkList reddit digg Del.icio.us

本文关键字本文关键字: J2ME  读取  Unicode  UTF-8  编码  

阅读数: 320 | 打印 | E-mail

  我要评论
RSS评论

发表评论

姓名:
E-mail
您的网站/主页
标题:
评论:

验证码:* Code
若有人评论本文,请E-mail通知我。

(使用J2mePolish的build功能) < 上一篇   下一篇 > (使用J2ME发送手机短信)