How to write data to a file in Java ME
This article shows, how to write data to a file by using FileConnection API (JSR-75). This example MIDlet has a TextBox, where text can be entered. Then the text can be saved to readme.txt file, which is in this case created under Gallery's Images folder.
Article Metadata
Code Example
Source file: WriteDataExample sources
Installation file: WriteDataExample binaries
Tested with
SDK: Nokia Asha SDK 1.0 (beta), Nokia SDK 1.1 for Java, Nokia SDK 2.0 for Java (beta), Nokia Symbian SDKs
Devices(s): Nokia Asha 501, Nokia C3-01, Nokia Asha 306, Nokia E7-00
Compatibility
Platform(s): Nokia Asha Platform 1.0 and later, Series 40 DP 1.0 and later, Nokia Belle
Article
Keywords: textBox,Commndbutton,String,FileConnection
Created: jarmlaht
(28 Jan 2009)
Reviewed: skalogir
(09 May 2013)
Last edited: skalogir
(13 May 2013)
Source code: WriteMIDlet.java
The full source code for a test MIDlet:
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 string = textbox.getString();
byte data[] = string.getBytes("UTF-8");
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();
}
}


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.?
tnxAnorEnaj2330 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
Hamishhamishwillee 08:30, 21 September 2012 (EEST)