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