import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class LoadText
extends MIDlet implements CommandListener {
private Display display;
private Form mainForm;
private TextField textField;
public void startApp() {
this.display = Display.getDisplay(this);
this.mainForm = new Form("本地ANSI).编码的文本");
this.textField = new TextField("靠自己", "hello", 10240, TextField.ANY);
this.mainForm.addCommand(new Command("离开", Command.EXIT, 0));
String str = loadText("/kaoziji(ANSI).txt");
this.textField.setString(str);
this.mainForm.append(this.textField);
this.mainForm.setCommandListener(this);
this.display.setCurrent(mainForm);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
this.notifyDestroyed();
}
//读取本地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编码的文本文件(没问题)
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-8");
}
catch (Exception e) {
System.out.println("读取本地UTF-8编码的文本出错:" + e.toString());
}
finally {
in = null;
}
return strReturn;
}
========================================
//读取本地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编码文本 (有问题)
private String loadText(String resource) {
byte[] word_uni_b_e = new byte[10240];
DataInputStream dis;
try {
dis = new DataInputStream(getClass().getResourceAsStream(resource));
dis.readFully(word_uni_b_e);
}
catch (Exception e) {
System.out.println("读取ANSI编码文本文件出错!" + e.getMessage());
}
finally {
dis = null;
}
return new String(word_uni_b_e);
}
====================================
平台(Sun J2ME Wireless Toolkit2.2)下面是执行结果截图:
ANSI编码:

Unicode编码:
UTF-8编码:
Unicode Big Endian编码: