0

I have the following code:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;

public class FileConnection extends MIDlet implements CommandListener, Runnable {
  private Command exit, start;
  private Display display;
  private Form form;
  public FileConnection () 
  {
    display = Display.getDisplay(this);
    exit = new Command("Exit", Command.EXIT, 1);
    start = new Command("Start", Command.EXIT, 1);
    form = new Form("Write To File");
    form.addCommand(exit);
    form.addCommand(start);
    form.setCommandListener(this);
  }
  public void startApp() throws MIDletStateChangeException 
  {
    display.setCurrent(form);
  }

  public void run(){
      try{
          javax.microedition.io.file.FileConnection filecon =
          (javax.microedition.io.file.FileConnection)
          Connector.open("file:///root1/photos/fisier.txt", Connector.WRITE);
    OutputStream out = filecon.openOutputStream();
    PrintStream output = new PrintStream( out );
    output.println( "This is a test." );
    out.close();
    filecon.close();
    Alert alert = new Alert("Completed", "Data Written", null, null);
    alert.setTimeout(Alert.FOREVER);
    alert.setType(AlertType.ERROR);
    display.setCurrent(alert);
      }
      catch( ConnectionNotFoundException error )
      {
        Alert alert = new Alert(
             "Error", "Cannot access file.", null, null);
        alert.setTimeout(Alert.FOREVER);
        alert.setType(AlertType.ERROR);
        display.setCurrent(alert);      
       }
       catch( IOException error )
       {
        Alert alert = new Alert("Error", error.toString(), null, null);
        alert.setTimeout(Alert.FOREVER);
        alert.setType(AlertType.ERROR);
        display.setCurrent(alert);      
       }
  }

  public void pauseApp() 
  {
  }
  public void destroyApp(boolean unconditional) 
  {
  }
  public void commandAction(Command command, Displayable displayable) 
  {
    if (command == exit) 
    {
      destroyApp(false);
      notifyDestroyed();
    }
    else if (command == start) 
    {
      new Thread(this).start();

    }
  }
}

As you can see, I'm trying to write something in a text file, from an emulator. I run that code in a separate thread, to avoid that warning at the runtime. I have in C:\Program Files\WTK2.5.2_01\j2mewtk_template\appdb\DefaultColorPhone\filesystem\root1\photos a file named fisier.txt. When I try to run this code, and press 'Start', I hit 'Yes' at the question 'J2ME... Midlet Suite wants to write the local file system. It's OK to update your files? YES/NO'. And I got on the screen java.io.IOException:, and nothing more!..

What's wrong? Why I got that error? I did not find anywhere a working code, of how to write to a local .txt file.
Don't know what's wrong in my code?

4

1 回答 1

1

可能是路径错误(并且文件不存在),您没有对它的写访问权,或者它在其他地方打开。

您是否尝试过调用FileConnection该类提供的一些方法,例如canWrite()exists()isOpen(),以查看其中一些常见问题是否适用于您的案例?

于 2010-03-07T20:02:59.080 回答