Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

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

From Wiki
Jump to: navigation, search

import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import*; import*; import*;

public class WriteMIDlet extends MIDlet implements CommandListener {

   private TextBox textbox;
   private String 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.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

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

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

-, bci=26
-, bci=104
-, bci=917
-, 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
-, bci=4
-, bci=29
-, bci=36
-, bci=80
-, bci=16
-, bci=498
-, bci=5
-, bci=47
-, bci=3
- file.WriteMIDlet.saveFile(
- file.WriteMIDlet.commandAction(
- javax.microedition.lcdui.Display$DisplayEventConsumerImpl.handleCommandEvent(), bci=296
- com.sun.midp.lcdui.DisplayEventListener.process(), bci=467
-, bci=182
-, 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)"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

-, bci=42
-, bci=406
-, bci=5
-, bci=47
-, bci=3
-, bci=2
- 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
-, bci=182
-, bci=5

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