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 over the next few weeks. Thanks for all your past and future contributions.

Revision as of 06:04, 30 July 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

FleConnection Example - JSR 75

From Wiki
Jump to: navigation, search

FileConnection (JSR75) allows Java ME access to removable storage devices, such as external memory cards that many of today’s devices support. It was specially developed based on mobile devices limitation so it is simple and lightweight. FileConnection gives the functionality to create, remove directories and files, list directory content, set permissions, get file information, and perform I/O operations on files.

Article Metadata
Created: _katy_ (10 Jun 2009)
Last edited: hamishwillee (30 Jul 2013)


All you have to know before writing files in mobile devices

  • Internal files (file systems on mobile devices or external memory cards) cannot be accessed and the reason is simple: Not all MIDP devices have file systems, and the creators may not want to expose them to applications.
  • Developers don’t have permission to write anything directly on root directories because of security reasons.
  • Symbian devices have a private directory where files can be written. If your MIDlet will be used with Series 40 devices, be careful because you don’t have this directory. However, don’t worry there are other directories where you can write files (for example: file:///c:/My files/Images/, file:///c:/My files/Tones/, etc).

What does our sample Code do?

Our MIDlet is a simple example of how to write a text file. Running our MIDlet you will see a textbox where you can write something and save it. The file called “thiago.txt” will be saved inside photo directory in a folder named “bruno”. You can run the MIDlet with your favorite IDE or with your mobile device.

Used classes and information

To read and write files we used FileConnection class that was specially created based on mobile devices limitations. Our device have to implement JSR 75 to run this MIDlet properly.

As used in J2SE we need to use streams for connection. To open a connection we use “” and to open a strem we can use openInputStream(), openOutputStream(), openDataInputStream(), or openDataOutputStream().

Pay special attention to always check file’s or directory’s existence after a connection is established to determine if the file or directory actually exists. You can face exceptions or erros if you forget this. The same holds true using delete() method, and developers should close the connection immediately after deletion to prevent exceptions from accessing a connection to a non-existent file or directory.

Inside the method we can use “file:///c:/My files/Images/” or System.getProperty(””), but it is better to use System.get.Property because from device to device this path can change so you will face some problems. If you don’t know the list of properties you can check it here

For further information about FileConnection check here


The full code is below to facilitate your understanding.

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
public class FileWriter extends MIDlet implements CommandListener {
private Command save, exit;
private TextBox text;
private Display display;
protected void destroyApp(boolean unconditional) {
// TODO Auto-generated method stub
protected void pauseApp() {
// TODO Auto-generated method stub
protected void startApp() {
if (display == null) {
display = Display.getDisplay(this);
text = new TextBox("", "", 400, TextField.ANY);
save = new Command("Save", Command.SCREEN, 1);
exit = new Command("Exit", Command.EXIT, 1);
public void commandAction(Command command, Displayable displayable) {
if (command == save) {
// Alert used for notify the user that the file had already been
// saved
Alert alert;
try {
alert = new Alert("File saved.");
} catch (Exception e) {
alert = new Alert(e.toString());
if (command == exit) {
private void saveFile() throws IOException {
String folder = System.getProperty("") + "bruno/";
// Creating a connection.
FileConnection c = (FileConnection), Connector.READ_WRITE);
try {
// Checking if the directoy exists or not. If it doesn't exist we
// create it.
if (c.exists()) {
} else {
System.out.println("nao existe");
} finally {
c = (FileConnection) + "thiago.txt", Connector.READ_WRITE);
try {
if (!c.exists()) {
// create the file
// create an OutputStream
OutputStream out = c.openOutputStream();
try {
// Get the user text
String userText = text.getString();
// write out the user's text into the file
} finally {
} finally {
// Never forget to close a connection or you can face problems.
// Pay attention here! If you close the connection before and
// later try to
// write something it will throw an exception.
This page was last modified on 30 July 2013, at 06:04.
78 page views in the last 30 days.

Was this page helpful?

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


Thank you!

We appreciate your feedback.