1

我想在 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 为您提供帮助。

4

2 回答 2

0

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

if (c==read) {

        new Thread(new Runnable() {

              public void run() {
                  try {
                      ReadFile(path);
                      } catch (IOException ex) {
                          ex.printStackTrace();
                      }
              }
        }).start();
    }
于 2013-07-15T20:51:59.907 回答
0

这是我的OpenFileDialogJava ME 版本。

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

于 2013-07-14T21:53:53.517 回答