Talk:How to write data to a file in Java ME

From Nokia Developer Wiki
Jump to: navigation, search

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

public class WriteMIDlet extends MIDlet implements CommandListener {

   private TextBox textbox;
   private String photos = "fileconn.dir.photos";
   private Command saveCommand;
   private Command exitCommand;
   private String path;

   public void startApp() {
       textbox = new TextBox("WriteMIDlet", "", 1000, TextField.ANY);
       saveCommand = new Command("Save", Command.SCREEN, 1);
       exitCommand = new Command("Exit", Command.EXIT, 1);
       path = System.getProperty(photos);

   public void pauseApp() {

   public void destroyApp(boolean unconditional) {

   private void saveFile(String path, String name) {
       try {
           //String url = path + name;
            String url=""file:///SDCard/A.TXT"";
           String string = textbox.getString();
           byte data[] = string.getBytes();
           FileConnection fconn = (FileConnection)Connector.open(url, Connector.READ_WRITE);
           if (!fconn.exists()) {
           OutputStream ops = fconn.openOutputStream();
       catch (IOException ioe) {
           System.out.println("IOException: "+ioe.getMessage());
       catch (SecurityException se) {
           System.out.println("Security exception:" + se.getMessage());

   public void commandAction(Command c, Displayable d) {
       if (c == saveCommand) saveFile(path, "readme.txt");
       if (c == exitCommand) this.notifyDestroyed();


the above code throwing an ioexception if i run it in 3100c nokia.. please help me..


Cedsadbe - Change path

Change the url path using "fileconn.dir.memorycard" in photos String.

cedsadbe 00:24, 17 November 2011 (EET)

Hamishwillee - Cedsadbe - does this need to be changed in the article itself?

If so, could you please update it?

hamishwillee 05:11, 29 November 2011 (EET)

AnorEnaj2330 - Where can i find that textfile

Good day!

so i try it to my j2me but it's not working where can i create a textfile . or its automatically created? you created it first in a folder or when you written those codes it's automatic created.?


AnorEnaj2330 05:14, 21 September 2012 (EEST)

Hamishwillee - @AnorEnaj2330 try the forums

Hi AnorEnaj2330

This was written 3 years ago, so the author may not be watching this article. I suggest you raise the issue in the Java Discussion board, linking to this article.



hamishwillee 08:30, 21 September 2012 (EEST)

Bolloons - I have a problem with connection.open()

I tied this code on the emulator but it is generating this exceptions:

java.lang.RuntimeException: Blocking call performed in the event thread

- com.sun.midp.security.PermissionDialog.waitForAnswer(), bci=26
- com.sun.midp.security.SecurityHandler.askUserForOneShotPermission(), bci=104
- com.sun.midp.security.SecurityHandler.checkForPermission(), bci=917
- com.sun.midp.security.SecurityHandler.checkForPermission(), bci=26
- com.sun.midp.midletsuite.MIDletSuiteImpl.checkForPermission(), bci=20
- com.sun.midp.midletsuite.MIDletSuiteImpl.checkForPermission(), bci=18
- com.sun.midp.main.CldcAccessControlContext.checkPermissionImpl(), bci=34
- com.sun.j2me.security.AccessControlContextAdapter.checkPermission(), bci=4
- com.sun.j2me.security.AccessController.checkPermission(), bci=29
- com.sun.j2me.app.AppPackage.checkForPermission(), bci=36
- com.sun.io.j2me.file.Protocol.checkPermission(), bci=80
- com.sun.io.j2me.file.Protocol.checkReadPermission(), bci=16
- com.sun.io.j2me.file.Protocol.openPrimImpl(), bci=498
- com.sun.io.j2me.file.Protocol.openPrim(), bci=5
- javax.microedition.io.Connector.open(), bci=47
- javax.microedition.io.Connector.open(), bci=3
- file.WriteMIDlet.saveFile(WriteMIDlet.java:37)
- file.WriteMIDlet.commandAction(WriteMIDlet.java:55)
- javax.microedition.lcdui.Display$DisplayEventConsumerImpl.handleCommandEvent(), bci=296
- com.sun.midp.lcdui.DisplayEventListener.process(), bci=467
- com.sun.midp.events.EventQueue.run(), bci=182
- java.lang.Thread.run(), bci=5

I also tried another approach more direct this one:

try {

String root= System.getProperty("fileconn.dir.private"); System.out.println(root); FileConnection fconn = (FileConnection)Connector.open("file:///SDCard/newfile.txt"); fconn.canWrite(); fconn.canRead(); // If no exception is thrown, then the URI is valid, but the file may or may not exist. if (!fconn.exists()) fconn.create(); // create the file if it doesn't exist

fconn.close(); } catch (IOException ioe) { ioe.printStackTrace(); }

the exception generated is this:

java.lang.SecurityException: Root cannot be accessible

- com.sun.io.j2me.file.Protocol.connect(), bci=42
- com.sun.io.j2me.file.Protocol.openPrimImpl(), bci=406
- com.sun.io.j2me.file.Protocol.openPrim(), bci=5
- javax.microedition.io.Connector.open(), bci=47
- javax.microedition.io.Connector.open(), bci=3
- javax.microedition.io.Connector.open(), bci=2
- com.nokia.example.RegScreen1.<init>(RegScreen1.java:80)
- com.nokia.example.LoginScreen$3.run(LoginScreen.java:95)
- com.nokia.example.Button.handlePointerEvent(Button.java:122)
- com.nokia.example.LoginScreen.pointerReleased(LoginScreen.java:170)
- javax.microedition.lcdui.CanvasLFImpl.uCallPointerReleased(), bci=10
- javax.microedition.lcdui.DisplayableLFImpl.uCallPointerEvent(), bci=129
- javax.microedition.lcdui.Display$DisplayEventConsumerImpl.handlePointerEvent(), bci=16
- com.sun.midp.lcdui.DisplayEventListener.process(), bci=337
- com.sun.midp.events.EventQueue.run(), bci=182
- java.lang.Thread.run(), bci=5

Bolloons (talk) 15:28, 24 June 2014 (EEST)


Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.