(源自:http://hi.baidu.com/wishwingliao/blog/item/220993dd184dab345982dd50.html)
import java.io.*; import javax.microedition.io.*;
//读取本地ANSI编码文本(没问题)
private String loadText(String yourfilename) { InputStream is = null; String strs = ""; try { Class c = this.getClass(); is = c.getResourceAsStream(yourfilename); InputStreamReader isr = new InputStreamReader(is); StringBuffer buffer = new StringBuffer(); int ch; while ((ch = isr.read()) > -1) { buffer.append((char)ch); } strs = buffer.toString(); if (isr != null) isr.close(); if(buffer != null) buffer = null; } catch(IOException e) { } return strs; }
========================================================================================
======================================================================================== //读取本地UTF-8编码的文本文件(没问题)
========================================================================================
//读取本地Unicode big endian编码文本文件(没问题)
public String loadText(String name) { String strReturn = ""; InputStream in = null; byte[] word_utf= new byte[1024]; try { in = getClass().getResourceAsStream(name); in.read(word_utf); in.close(); strReturn=new String(word_utf,"UTF-16BE");//后一个参数改为"UTF-8"即可读取UTF-8编码的 } catch(Exception e) { System.out.println(e.toString()); } finally { in = null; } return strReturn; }
========================================================================================
//读取Unicode big endian编码文本(有问题)
private String loadText(String resource) { char[] word_uni_b_e = new char[1024]; String strReturn=""; DataInputStream dis; try { dis = new DataInputStream(getClass().getResourceAsStream(resource)); int counter = 0; dis.skip(2); char temp; while(true) { temp = dis.readChar(); if(temp == '$') { break; } word_uni_b_e[counter++] = temp; } dis.close(); strReturn = String.valueOf(word_uni_b_e,0,counter); } catch(Exception e) { System.out.println(" 读取Unicode big endian编码文本文件出错!" + e.getMessage()); } finally { dis = null; } return strReturn; }
读取Unicode big endian编码文本文件出错!null
不知何因
========================================================================================
//读取ANSI编码文本 (有问题) }
========================================================================================
平台(Sun J2ME Wireless Toolkit2.2)下面是执行结果截图:
ANSI编码: Unicode编码:
UTF-8编码: Unicode Big Endian编码:
|