×
Namespaces

Variants
Actions

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);
       textbox.addCommand(saveCommand);
       textbox.addCommand(exitCommand);
       textbox.setCommandListener(this);
       Display.getDisplay(this).setCurrent(textbox);
       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()) {
               fconn.create();
           }
           OutputStream ops = fconn.openOutputStream();
           ops.write(data);
           ops.close();
           fconn.close();
       }
       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..

Contents

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.?

tnx

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.

Regards

Hamish

hamishwillee 08:30, 21 September 2012 (EEST)