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

王朝java/jsp·作者佚名  2007-05-09
窄屏简体版  字體: |||超大  

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

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航