广嵌科技  广嵌教育  广嵌威客  English  
 
首页 > 软件开发 > J2EE > J2ME中读取Unicode和UTF-8编码文件
J2ME中读取Unicode和UTF-8编码文件
来源:java.chinaitlab.com 作者: 时间:2007-06-04 发布人:清雨

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

}


·下一条:揭开J2EE集群Cluster的神秘面纱

评论
发布者 标题 发布日期
暂没有任何评论
以下发言只是广嵌网会员个人意见,非本网立场 查看更多评论

发表评论
·尊重网上道德,遵守中华人民共和国的各项有关法律法规。
·承担一切因您的行为而直接或间接导致的民事或刑事法律责任。
·本站管理人员有权保留或删除其管辖留言中的任意内容,本站有权在网站内转载或引用您的评论。
·参与本评论即表明您已经阅读并接受上述条款。
标题:
发布者:
内容:
验证码:

相关信息
Copyright ©2005-2007 广东省嵌入式软件公共技术中心.All Rights Reserved.版权所有 复制必究
客户服务支持:020-32068395-832 24小时服务热线:13631411558
技术支持与报障:gdesc@midea.com.cn 020-32068395-807/809
粤ICP备05104135号