科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道J2ME中读取Unicode和UTF-8编码文件

J2ME中读取Unicode和UTF-8编码文件

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

一、读取Unicode文件 /** * 读取Unicode编码文本文件 * @param resource String - 文件名 * @return String - Unicode文本 *

作者:中国IT实验室 来源:中国IT实验室 2007年9月18日

关键字: 读取 J2ME 编程 java

  • 评论
  • 分享微博
  • 分享邮件

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

   }

 

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    闁喕娆㈢拋銏ゆ

    婵″倹鐏夐幃銊╂姜鐢瓕鎻╅崚鍥╂畱閹厖绨$憴顤廡妫板棗鐓欓張鈧弬棰侀獓閸濅椒绗岄幎鈧張顖欎繆閹垽绱濋柇锝勭疄鐠併垽妲勯懛鎶姐€婄純鎴炲Η閺堫垶鍋栨禒璺虹殺閺勵垱鍋嶉惃鍕付娴f娊鈧柨绶炴稊瀣╃閵嗭拷

    重磅专题
    往期文章
    最新文章