File 在屏幕上显示行(读取j2me文件)?

File 在屏幕上显示行(读取j2me文件)?,file,java-me,File,Java Me,我想在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.

我想在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();
    } 
}
我放这行代码来测试哪一行导致错误

AlertType.ERROR.playSound(display);

这行似乎无法运行。我也阅读了这里的帖子(stackOverflow),但我无法解决我的问题。tnx为您提供帮助。

这是我的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);
}
public类OpenFileDialog扩展列表
实现CommandListener
{
公共静态最终字符串前缀=“文件://”;
私有静态最终字符串UP=“[…]”;
私有静态最终字符串DIR=“+”;
私有堆栈=新堆栈();
私有OpenFileListener侦听器;
私有命令侦听器命令侦听器;
私有字符串扩展;
公共OpenFileDialog(OpenFileListener侦听器,
字符串标题、字符串扩展名)
{
超级(标题,列表,隐式);
super.setCommandListener(this);
this.listener=listener;
这个扩展=扩展;
addRoots();
}
公共void setCommandListener(CommandListener l){
this.commandListener=l;
}
公共无效命令操作(命令c,可显示d){
if(c==List.SELECT_命令){
这个.changeSelection();
}否则{
if(this.commandListener!=null){
this.commandListener.commandAction(c,d);
}
}
}
私有字符串getSelectedText(){
int index=getSelectedIndex();
如果(索引<0 | |索引>=this.size()){
返回“”;
}
返回此.getString(索引);
}
私有无效更改选择(){
String target=this.getSelectedText();
字符串parent=null;
布尔goingUp=false;
如果(上升等于(目标)){
goingUp=真;
stack.pop();
if(stack.isEmpty()==false){
target=(字符串)stack.peek();
}否则{
super.deleteAll();
添加根();
此.setTicker(null);
返回;
}
}否则{
if(stack.isEmpty()==false){
父项=stack.peek().toString();
}
}
试一试{
if(目标启动(DIR)){
target=target.substring(3);
}
如果(父项!=null){
目标=家长+目标;
}
这个.setTicker(新的Ticker(目标));
FileConnection fc=(FileConnection)
Connector.open(前缀+目标,Connector.READ);
if(fc.isDirectory()){
super.deleteAll();
如果(goingUp==false){
堆栈推送(目标);
}
super.append(UP,null);
枚举项=fc.list();
while(entries.hasMoreElements()){
String entry=(String)entries.nextElement();
文件连接fc2=(文件连接)
连接器。打开(前缀+目标+条目,
连接器(读取);
if(fc2.isDirectory()){
super.append(DIR+entry,null);
}else if(entry.toLowerCase().endsWith(扩展名)){
super.append(条目,空);
}
fc2.close();
}
fc.close();
}否则{
this.listener.fileSelected(fc);
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
私有void addroot(){
枚举根=FileSystemRegistry.listRoots();
while(root.hasMoreElements()){
super.append(DIR+root.nextElement().toString(),null);
}
}
}
接口OpenFileListener{
选择的无效文件(文件连接fc);
}

它第一次出现在

我发现了我的问题,也许这也是你的问题,我只是在commandAction函数中使用了线程,它就工作了。以下是更改的代码:

if (c==read) {

        new Thread(new Runnable() {

              public void run() {
                  try {
                      ReadFile(path);
                      } catch (IOException ex) {
                          ex.printStackTrace();
                      }
              }
        }).start();
    }

tnx为你的答案,但我想知道为什么这个代码没有工作,这是非常简单的代码请帮助。再次感谢