tags:

views:

344

answers:

1

Hello,

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 sepparated 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, can you help me?

Thanks!

Thanks!

A: 

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?

funkybro
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?
qwerty
Why the path is not correct? How should i rewrite the path?
qwerty
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.
funkybro