开发者

J2ME java.io.IOException error

开发者 https://www.devze.com 2022-12-22 00:23 出处:网络
I have the following code: import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.io.*;

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 ale开发者_开发技巧rt = 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?


Could be anything from getting the path wrong (and the file not existing), to you not having write access to it, to it being open elsewhere.

Have you tried calling some of the methods the FileConnection class offers, such as canWrite(), exists(), and isOpen(), to see if some of these common problems apply in your case?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号