×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    File Writing Nokia 6212 NFC

    Dear Forum,

    I have some problems according to file handling onto the Nokia 6212 cell phone device:
    My intention was simply to write file. I tried two approaches:
    http://www.java-tips.org/java-me-tip...to-a-file.html
    and
    http://www.forum.nokia.com/forum/sho...d.php?t=185491.

    For the latter one the Class FileConnection didn't work. Perhaps it's not an included package in the Nokia 6212 SDK as in http://developers.sun.com/mobility/a...ileconnection/ section The File Connection APIs described.

    For the first one, when I try to write to a file, I get a prompt asking for the permission to access Gallery. Replying 'Yes' or 'No' closes the MIDlet with the output:
    'Security java/lang/SecurityException Access denied'.

    This is the implemented code:

    Code:
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import java.io.*;
    import javax.microedition.io.*;
    
    public class FileTest extends MIDlet implements CommandListener{
    
    	  private Command exit, start;
    	  private Display display;
    	  private Form form;
    	public FileTest() {
    	    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 pauseApp() 
    	  {
    	  }
    	  public void destroyApp(boolean unconditional) 
    	  {
    	  }
    	  public void commandAction(Command command, Displayable displayable) 
    	  {
    	    if (command == exit) 
    	    {
    	      destroyApp(false);
    	      notifyDestroyed();
    	    }
    	    else if (command == start) 
    	    {
    	      try 
    	      {
    	        OutputConnection connection = (OutputConnection)                     
    	          Connector.open("file://c:/FileTest.jad;append=true", Connector.WRITE );
    	        OutputStream out = connection.openOutputStream();
    	        PrintStream output = new PrintStream( out );
    	        output.println( "This is a test." );
    	        out.close();
    	        connection.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);      
    	        }
    	    }
    	  }
    	}
    The JAD file looks like this:

    Code:
    MIDlet-1: FileTest,,wikassel.FileTest
    MIDlet-Jar-URL: FileTester.jar
    MicroEdition-Configuration: CLDC-1.0
    MIDlet-Version: 1.0.0
    MIDlet-Name: FileTester MIDlet Suite
    MIDlet-JAR-SIZE: 100
    MIDlet-Vendor: MIDlet Suite Vendor
    MicroEdition-Profile: MIDP-2.1
    MIDlet-Permissions: javax.microedition.io.Connector.comm,javax.microedition.io.Connector.datagram,javax.microedition.io.Connector.datagramreceiver,javax.microedition.io.Connector.http,javax.microedition.io.Connector.https,javax.microedition.io.Connector.ssl,javax.microedition.io.Connector.serversocket,javax.microedition.io.Connector.socket,javax.microedition.io.PushRegistry,javax.microedition.io.Connector.sms,javax.microedition.io.Connector.cbs,javax.microedition.io.Connector.mms,javax.wireless.messaging.sms.send,javax.wireless.messaging.sms.receive,javax.wireless.messaging.cbs.receive,javax.wireless.messaging.mms.send,javax.wireless.messaging.mms.receive, javax.microedition.io.Connector.file.read, javax.microedition.io.Connector.file.write
    I toggled MIDlet-JAR-SIZE and MIDlet-Permissions on and off, so this it can't be.

    Would be very nice if you could help me :-)

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: File Writing Nokia 6212 NFC

    MIDlet-Jar-Size must equal the size of the JAR file, in bytes.

    What do you mean by "the Class FileConnection didn't work"? Can you be more specific? (Didn't compile, didn't install, didn't run, what?)

    "Access denied" means you are not permitted to do what you are trying. If you're trying to write to the file system, this means your MIDlet do not have permission to write to that part of the file system (possible to any part, depending on device).

    In your case, you are trying to write to the root of the file system, which is not allowed.

    Also, don't use this ";append=true" syntax. I'm not sure it works. Use FileConnection.openOutputStrea(offset), using FileConnection.fileSize() to find the length of the file to use as the offset.

    You can find information about the FileConnection API here.

    Graham.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    Re: File Writing Nokia 6212 NFC

    Hi Graham,

    thank you very much for the fast reply.

    Quote Originally Posted by grahamhughes View Post
    MIDlet-Jar-Size must equal the size of the JAR file, in bytes.
    Now I left the row MIDlet-Jar-Size.

    Quote Originally Posted by grahamhughes View Post

    What do you mean by "the Class FileConnection didn't work"? Can you be more specific? (Didn't compile, didn't install, didn't run, what?)
    My mistake. Now the code runs and includes the library.

    Quote Originally Posted by grahamhughes View Post
    In your case, you are trying to write to the root of the file system, which is not allowed.
    Do you know how I get the path in which the MIDlets runs, something like pwd in Unix?
    I just found absolute path descriptions.

    The following Code throws the same Security Access Exception:

    Code:
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import java.io.*;
    import javax.microedition.io.*;
    import javax.microedition.io.file.*;
    
    public class FileTest extends MIDlet implements CommandListener{
    
    	  private Command exit, start;
    	  private Display display;
    	  private Form form;
    	public FileTest() {
    	    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 pauseApp() 
    	  {
    	  }
    	  public void destroyApp(boolean unconditional) 
    	  {
    	  }
    	  public void commandAction(Command command, Displayable displayable) 
    	  {
    	    if (command == exit) 
    	    {
    	      destroyApp(false);
    	      notifyDestroyed();
    	    }
    	    else if (command == start) 
    	    {
    	      try 
    	      {
    	        FileConnection fileConnection = (FileConnection) Connector.open(
    	    		    "file:///C:/Programme/Spiele/FileTester.jad", Connector.WRITE);
    	        OutputStream outputStream = fileConnection.openOutputStream(Long.MAX_VALUE);
    	        PrintStream printStream = new PrintStream(outputStream);
    	        printStream.println("Some text to append");
    	       } catch (IOException e) 
    	       {
    	    		  e.printStackTrace();
    	       }	    }
    	  }
    	}
    This is the Jad-File

    Code:
    MIDlet-1: FileTest,,wikassel.FileTest
    MIDlet-Jar-URL: FileTester.jar
    MicroEdition-Configuration: CLDC-1.0
    MIDlet-Version: 1.0.0
    MIDlet-Name: FileTester
    MIDlet-Vendor: MIDlet Suite Vendor
    MicroEdition-Profile: MIDP-2.1
    MIDlet-Permissions: javax.microedition.io.Connector.comm,javax.microedition.io.Connector.datagram,javax.microedition.io.Connector.datagramreceiver,javax.microedition.io.Connector.http,javax.microedition.io.Connector.https,javax.microedition.io.Connector.ssl,javax.microedition.io.Connector.serversocket,javax.microedition.io.Connector.socket,javax.microedition.io.PushRegistry,javax.microedition.io.Connector.sms,javax.microedition.io.Connector.cbs,javax.microedition.io.Connector.mms,javax.wireless.messaging.sms.send,javax.wireless.messaging.sms.receive,javax.wireless.messaging.cbs.receive,javax.wireless.messaging.mms.send,javax.wireless.messaging.mms.receive, javax.microedition.io.Connector.file.read, javax.microedition.io.Connector.file.write
    I think the problem is in the path description, right?

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: File Writing Nokia 6212 NFC

    The JAD file must contain the MIDlet-Jar-Size attribute, or it is not a valid JAD, and the application will not install on most devices.

    OK... no, the problem is your entire premise.

    1. You cannot get the "current working directory". There may not even be such a concept on the host operating systems.

    2. You cannot find the folder in which the JAD and JAR are installed.

    3. After installation, depending on device, the JAD and JAR might not even exist as files (they might have been unpacked, or converted into some other form).

    4. Even if the JAD and JAR exist as files, and you can find where they are, you will not be permitted to access these files, or any files in this part of the file system.

    It looks like you're trying to modify your own JAD? There is absolutely no way you can do this. Why do you want to?? What are you actually trying to achieve?

    Graham.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    Re: File Writing Nokia 6212 NFC

    Mmh, ok, this looks bad.

    I saved some data descriptions in the Jad of another Midlet I implemented.
    My intention is to change these data description with this Midlet, so that I can modify the data.

    Perhaps there are some other solutions. The things which comes to my mind are:

    1. Creating a text file as data repository into the device and use it. Thought of it, to make use of it in the same way mobile games store their results somewhere and get it back.

    2. Modify the JAD in the Midlet which is using the data of it. There the cat is biting in its tail.

    The things I won't do are:

    1. Get the data from the net every time the midlet is started.

    2. Provide the user with a desktop application so he can manually change the JAD-File and has to upload it on his own on the device.

    Perhaps a certificate is unevitable?


    I would be very appreciated if there would be an easy workflow of doing this.

  6. #6
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: File Writing Nokia 6212 NFC

    OK. I understand something of what you're trying to achieve now.

    Mobile games store data using the Record Management System (RMS). Check the JavaDocs for the package javax.microedition.rms. RMS provides MIDlets with private storage (it is generally inaccessible to other MIDlets, and often to other application on the device, even native applications). The RMS API is a standard part of MIDP-1 and later, so is not dependent on devices having an optional API (like JSR-75). Since it is private to the MIDlet, it is not subject to the security problems that plague JSR-75 - there will be no confirmation prompts when you read or write RMS data, you will not need digital signing or any special JAD options.

    You describe changing the data in one MIDlet from another... while this is technically possible with RMS, it is not simple. Shared RMS is supported only on MIDP-2.0 and later, and is subject to security restrictions. RMS is best used for data an application wants to keep persistently for itself between executions. Examples are high-scores, user preferences and so on. Also, data downloaded over the network can be stored in RMS.

    There are many threads in the forum that discuss the use of RMS... search for "RecordStore".

    Does that sound useful to you?

    Graham.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    Re: File Writing Nokia 6212 NFC

    Ah, excellent. I'm going to try this.

    Thank you a lot

  8. #8
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: File Writing Nokia 6212 NFC

    There's some stuff about using RMS for high-scores in this thread, which you might find interesting.

    Graham.
    Last edited by grahamhughes; 2010-03-28 at 18:18.

Similar Threads

  1. Nokia Image Converter
    By davidpurdie in forum General Development Questions
    Replies: 0
    Last Post: 2004-02-18, 15:31
  2. Series 60Series 60 MIDP Concept SDK Beta 0.2 Linux bug?
    By kauppi in forum Mobile Java Tools & SDKs
    Replies: 3
    Last Post: 2003-04-07, 09:05
  3. Http connection problem in 6310i
    By teahola in forum Mobile Java General
    Replies: 1
    Last Post: 2002-10-03, 18:46

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •