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?

1
  • when you catch the exception, call Throwable.printStackTrace() to make sure you know what method actually throws it. Use Connector.READ_WRITE. Make sure your emulator security policy gives full access to the file system when running an unsigned MIDlet (which I assume yours is). Close the PrintStream first. update your question with the results. Commented Mar 8, 2010 at 14:43

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

2 Comments

I tried: public void run(){ try{ javax.microedition.io.file.FileConnection filecon = (javax.microedition.io.file.FileConnection) Connector.open("file:///root1/photos/fisier.txt", Connector.READ_WRITE); System.out.println(filecon.exists()); and i got 'false'. But that file exists! (and it's empty). Why i got false there?
I have no idea where your emulator saves your files. Try creating the file using filecon.create(), and then search your filesystem to see where the file has been put.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.