读取TXT文档并显示的问题
本人刚学J2ME,请高手帮忙.
TXT文档格式如下:
啊 a1
阿 a2
......
唉 ai1
就是一个拼音与汉字的对应表,想从表中读取这些信息逐行显示。代码如下,但有报错。
public class ReadDate extends MIDlet{
private Display display;
private TextBox textBox;
public ReadDate(){
display=Display.getDisplay(this);
String s= "sadfsdf ";
String str = " ";
textBox=new TextBox( "display ",str,50,0);
InputStream in = this.getClass().getResourceAsStream( "pinyinbiao.txt ");
int c;
ByteArrayOutputStream bais = new ByteArrayOutputStream();
try {
while((c=in.read())!= -1)
{
bais.write(c);
}
byte data[] = bais.toByteArray();
str = new String(data, "utf-8 ");
} catch (IOException e) {
e.printStackTrace();
}
}
protected void startApp(){display.setCurrent(textBox);}
protected void destroyApp(boolean unconditional){}
protected void pauseApp(){}
}
错误信息如下:
java.lang.NullPointerException
at ReadDate. <init> (+55)
at java.lang.Class.runCustomCode(+0)
at com.sun.midp.midlet.MIDletState.createMIDlet(+19)
at com.sun.midp.midlet.Selector.run(+22)
Execution completed.
734647 bytecodes executed
26 thread switches
741 classes in the system (including system classes)
4020 dynamic objects allocated (117744 bytes)
2 garbage collections (90528 bytes collected)
Execution completed.
734431 bytecodes executed
26 thread switches
741 classes in the system (including system classes)
4020 dynamic objects allocated (117476 bytes)
2 garbage collections (90368 bytes collected)
哪位能给解释一下是什么原因,还有就是例如报错的信息中at ReadDate. <init> (+55),是什么意思,怎么去修改和查找这些错误,谢谢高手指教。
[解决办法]
ByteArrayOutputStream bais = new ByteArrayOutputStream();
==========================
读数据要用
ByteArrayInputStream
[解决办法]
InputStream in = this.getClass().getResourceAsStream( "/pinyinbiao.txt ");
文件路径前注意加个 '/ '