我想在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);
似乎这行不能运行。我也在这里读取POSTS(stackOverflow),但我无法解决我的问题。 tnx for ur help。
答案 0 :(得分:0)
这是Java ME的OpenFileDialog
版本。
public class OpenFileDialog extends List
implements CommandListener
{
public static final String PREFIX = "file:///";
private static final String UP = "[ .. ]";
private static final String DIR = " + ";
private Stack stack = new Stack();
private OpenFileListener listener;
private CommandListener commandListener;
private String extension;
public OpenFileDialog (OpenFileListener listener,
String title, String extension)
{
super(title, List.IMPLICIT);
super.setCommandListener(this);
this.listener = listener;
this.extension = extension;
addRoots();
}
public void setCommandListener(CommandListener l) {
this.commandListener = l;
}
public void commandAction(Command c, Displayable d) {
if (c == List.SELECT_COMMAND) {
this.changeSelection();
} else {
if (this.commandListener != null) {
this.commandListener.commandAction(c, d);
}
}
}
private String getSelectedText () {
int index = getSelectedIndex();
if (index < 0 || index >= this.size()) {
return "";
}
return this.getString(index);
}
private void changeSelection () {
String target = this.getSelectedText();
String parent = null;
boolean goingUp = false;
if (UP.equals(target)) {
goingUp = true;
stack.pop();
if (stack.isEmpty() == false) {
target = (String) stack.peek();
} else {
super.deleteAll();
addRoots();
this.setTicker(null);
return;
}
} else {
if (stack.isEmpty() == false) {
parent = stack.peek().toString();
}
}
try {
if (target.startsWith(DIR)) {
target = target.substring(3);
}
if (parent != null) {
target = parent + target;
}
this.setTicker(new Ticker(target));
FileConnection fc = (FileConnection)
Connector.open(PREFIX + target, Connector.READ);
if (fc.isDirectory()) {
super.deleteAll();
if (goingUp == false) {
stack.push(target);
}
super.append(UP, null);
Enumeration entries = fc.list();
while (entries.hasMoreElements()) {
String entry = (String) entries.nextElement();
FileConnection fc2 = (FileConnection)
Connector.open(PREFIX + target + entry,
Connector.READ);
if (fc2.isDirectory()) {
super.append(DIR + entry, null);
} else if (entry.toLowerCase().endsWith(extension)) {
super.append(entry, null);
}
fc2.close();
}
fc.close();
} else {
this.listener.fileSelected(fc);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void addRoots() {
Enumeration roots = FileSystemRegistry.listRoots();
while (roots.hasMoreElements()) {
super.append(DIR + roots.nextElement().toString(), null);
}
}
}
interface OpenFileListener {
void fileSelected(FileConnection fc);
}
首次在http://smallandadaptive.blogspot.com.br/2009/08/open-file-dialog.html
展示答案 1 :(得分:0)
我发现了我的问题,也许这也是你的问题,我只是在commandAction函数中使用线程并且它可以工作。这是改变的代码:
if (c==read) {
new Thread(new Runnable() {
public void run() {
try {
ReadFile(path);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}).start();
}