读书人

j2me中读写文件的有关问题

发布时间: 2012-03-27 13:44:24 作者: rapoo

请教各位j2me中读写文件的问题

try{
InputStream inputStream = this.getClass().getResourceAsStream( "iconname.txt ");
stringBuffer = new StringBuffer();

int input;
while ((input = inputStream.read()) != -1){
if (input != '\n '){
stringBuffer.append((char)input);
}else{
iconNameVector.addElement(stringBuffer.toString());
stringBuffer = new StringBuffer();
}
}
inputStream.close();
connect.close();

}catch(Exception ex){
System.out.println( "the file connection could not be established ");
}

这种情况下会出现错误java.lang.NullPointerException

try{
connect = (StreamConnection)Connector.open( "http://localhost:8080/iconname.txt ", Connector.READ);
inputStream = connect.openInputStream();

stringBuffer = new StringBuffer();

int input;
while ((input = inputStream.read()) != -1){
if (input != '\n '){


stringBuffer.append((char)input);
}else{
iconNameVector.addElement(stringBuffer.toString());
stringBuffer = new StringBuffer();
}
}
inputStream.close();
connect.close();

}catch(Exception ex){
System.out.println( "the file connection could not be established ");
}

这种情况下会半天没有反应最后出现连结超时。

try{
connect = (StreamConnection)Connector.open( "file://C:/text/iconname.txt ", Connector.READ);
inputStream = connect.openInputStream();

stringBuffer = new StringBuffer();

int input;
while ((input = inputStream.read()) != -1){
if (input != '\n '){
stringBuffer.append((char)input);
}else{
iconNameVector.addElement(stringBuffer.toString());
stringBuffer = new StringBuffer();
}
}


inputStream.close();
connect.close();

}catch(Exception ex){
System.out.println( "the file connection could not be established ");
}

这种情况下会出现错误javax.microedition.io.ConnectionNotFoundException:Protocol not found:null

我已经试过好几次情况了,请各位帮我看看,为什么老是在连结读取文件的时候报错,谢谢!




[解决办法]
1.访问jar包内资源,以 "/ "开头,需修改为 "/iconname.txt "
2.确认本机8080端口提供http服务,并且在服务器根目录下存在 "iconname.txt " 文件
3.j2me还不支持 file 协议
[解决办法]
文件路径错了:
应该是file:///C:/text/iconname.txt
而且在模拟器上也没有C:这个根

读书人网 >J2ME开发

热点推荐