日期:2014-05-20  浏览次数:20792 次

J2ME中打开文件提示出现异常
J2ME中读取文件时总是出现以下异常:
  java.io.IOException
at com.sun.midp.io.j2me.file.Protocol.openInputStream(+39)
at com.sun.midp.io.j2me.file.Protocol.openInputStream(+9)
at org.readBook.myBook$ValidateTehread.run(+17)
这是怎么一回事啊?下面是代码,点击“阅读”按钮后,另一个窗口readForm弹不出来,并且提示上面的异常,关键是本来还好好的,可以运行,突然就莫名其妙不知道怎么的就成这样了,真真是郁闷了。。。

public class myBook extends List implements CommandListener {
private Command read = new Command("阅读",Command.SCREEN,1);
private readMIDlet parent;

public myBook(readMIDlet parent) {
super("我的书架",List.IMPLICIT);
this.parent=parent;
this.addCommand(read);
this.setCommandListener(this);
}

public void commandAction(Command c, Displayable d) {
if(c==read){
ValidateTehread vt = new ValidateTehread();
vt.start();
}
}


class ValidateTehread extends Thread{
public void run(){
try{
String fileUrl = "file:///root1/跨过千年来爱你.txt";
FileConnection fcFile = (FileConnection)Connector.open(fileUrl);
InputStream is = fcFile.openInputStream();
byte[] buffer = new byte[300];
int n = is.read(buffer,0,buffer.length);
String s = new String(buffer,0,n);
parent.changeForm("readForm", s); //转换到阅读窗口
fcFile.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}

------解决方案--------------------
try {
FileConnection fc = (FileConnection) Connector.open("file://root1/jason.txt", Connector.READ_WRITE);
InputStream fis = fc.openInputStream();
byte[] b = "Hello World".getBytes();
OutputStream fos = fc.openOutputStream();

fos.write(b, 0, b.length);
} catch (IOException e) {

e.printStackTrace();
}
return toString();
}
楼主可以看看这个…