我想在 j2me 中读取一个文件并在屏幕上显示它我尝试了很多,确切地说我尝试了 web 中存在的所有代码但没有人工作。这只是其中之一:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package file;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.file.*;
import javax.microedition.io.*;
import java.io.*;
/**
* @author ZARA-T
*/
public class ReadMidlet extends MIDlet implements CommandListener {
private Display display;
private Form form;
private Command read, exit;
StringBuffer buff;
public void startApp(){
display = Display.getDisplay(this);
read = new Command("Read", Command.EXIT, 1);
exit = new Command("Exit", Command.EXIT, 1);
form = new Form("Read File");
form.addCommand(exit);
form.addCommand(read);
form.setCommandListener(this);
display.setCurrent(form);
}
public void pauseApp(){}
public void destroyApp(boolean unconditional){
notifyDestroyed();
}
public void commandAction(Command c, Displayable s){
if (c==read) {
try {
String SS;
SS=ReadFile("file:///root1//hello.txt");
TextBox input = new TextBox("Enter Some Text:", "", 5, TextField.ANY);
input.setString(SS);
display.setCurrent(input);
} catch (IOException ex) {
ex.printStackTrace();
}
}
if (c==exit) destroyApp(false);
}
private String ReadFile(String url) throws IOException {
FileConnection fc = (FileConnection) Connector.open(url,Connector.READ);
AlertType.ERROR.playSound(display);
StringBuffer sb = new StringBuffer();
try {
InputStream in = fc.openInputStream();
try {
int i;
while ((i = in.read()) != -1) {
sb.append((char) i);
}
} finally {
in.close();
}
} finally {
fc.close();
}
form.append(sb.toString());
return sb.toString();
}
}
我把这个 CODE 行用来测试哪一行导致错误。
AlertType.ERROR.playSound(display);
似乎这条线无法运行。我也在这里阅读了帖子(stackOverflow),但我无法解决我的问题。tnx 为您提供帮助。