Would you like to tell us how we are doing?

You bet No thanks

Sorry about the red box, but we really need you to update your browser. Read this excellent article if you're wondering why we are no longer supporting this browser version. Go to Browse Happy for browser suggestions and how to update.

Storing files in the file system

You can programmatically create and manage the files and folders on BlackBerry smartphones with the File Connection API. The File Connection API was introduced with BlackBerry Device Software 4.2.

The File Connection API is implemented in the javax.microedition.io.file package.

The File Connection API is defined by JSR 75 and is built on the Generic Connection Framework. The main component of the File Connection API is the javax.microedition.io.file.FileConnection class. Unlike other Generic Connection Framework connections, FileConnection objects can be successfully returned from the javax.microedition.io.Connector.open() method without referencing an existing file or folder. This behavior allows for the creation of new files and folders on a file system. In addition to RIM documentation, there are many sources of information about JSR 75 and the Generic Connection Framework.

In addition, RIM provides extensions to the File Connection API. The net.rim.device.api.io.file package includes the following class and interfaces:

You can access the file system on internal storage and external media card storage:

Internal storage

Internal storage is application storage or built-in media storage. All BlackBerry smartphones have internal storage. To access internal storage, use the path file:///store. For example,

FileConnection fc = (FileConnection)Connector.open("file:///Store")

External storage

You can access external media card storage only on smartphones with microSD cards. To access external media card storage, use the path file:///SDCard. For example,

FileConnection fc = (FileConnection)Connector.open("file:///SDCard")

Files created by your app are not automatically deleted when your app is uninstalled.

Smartphones that have built-in media storage have a file system partition called System. You can access this partition with the path file:///system. In BlackBerry Device Software 5.0 and later, the system partition is reserved for system use and is read-only. In BlackBerry Device Software versions earlier than 5.0, the system partition is read/write.

Code sample: Creating a folder

import net.rim.device.api.system.Application;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import java.io.IOException;

public class CreateFolderApp extends Application 
{
    public static void main(String[] args) 
    {
        CreateFolderApp app = new CreateFolderApp();
        app.setAcceptEvents(false);
        try 
        {    // the final slash in the folder path is required
             FileConnection fc = (FileConnection)Connector.open("file:///SDCard/testfolder/");
             // If no exception is thrown, the URI is valid but the folder may not exist.
             if (!fc.exists())
             {
                 fc.mkdir();  // create the folder if it doesn't exist
             }
             fc.close();
         }
         catch (IOException ioe) 
         {
            System.out.println(ioe.getMessage() );
         }
    }
}

Code sample: Creating a file

import javax.microedition.io.*;
import java.io.IOException;
import javax.microedition.io.file.*;
import net.rim.device.api.system.Application.*;

public class CreateFileApp extends Application 
{
   public static void main(String[] args) 
   {
      CreateFileApp app = new CreateFileApp();
      app.setAcceptEvents(false);
      try 
      {
          FileConnection fc = 
               (FileConnection)Connector.open("file:///store/home/user/newfile.txt");
          // If no exception is thrown, the URI is valid but the file might not exist.
          if (!fc.exists())
          {
              fc.create();  // create the file if it doesn't exist
          }
          fc.close();
       }
       catch (IOException ioe) 
       {
          System.out.println(ioe.getMessage() );
       }
  }
}

Code sample: Writing text to a file

import net.rim.device.api.system.Application;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import java.io.IOException;
import java.io.OutputStream;

public class AddFileContent extends Application 
{
  public static void main(String[] args) 
  {
    AddFileContent app = new AddFileContent();
    app.setAcceptEvents(false);
    try 
    {
      FileConnection fc = (FileConnection)Connector.open("file:///store/home/user/newfile.txt");
      // If no exception is thrown, then the URI is valid, but the file may or may not exist.
      if (!fc.exists())
      {
          fc.create();  // create the file if it doesn't exist
      }
      OutputStream outStream = fc.openOutputStream(); 
      outStream.write("test content".getBytes());
      outStream.close();
      fc.close();
     }
     catch (IOException ioe) 
     {
        System.out.println(ioe.getMessage() );
     }
  }
}

Code sample: Reading sections of a binary file

This code sample demonstrates how to read sections of a binary file by reading header information from a .gif file. The app reads the width and height of the image from the header. To run the code sample, place a .gif file in the root folder of a media card in a BlackBerry smartphone.

import net.rim.device.api.ui.*;
import net.rim.device.api.io.*;
import javax.microedition.io.file.*;
import javax.microedition.io.*;
import java.io.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;

public class RandomFileAccess extends UiApplication
{
   public static void main(String[] args)
   {
      RandomFileAccess app = new RandomFileAccess();
      app.enterEventDispatcher();
   }
   public RandomFileAccess()
   {
      pushScreen(new HomeScreen());
   }
}

class HomeScreen extends MainScreen
{

   public HomeScreen()
   {
      setTitle("Random File Access Sample");
      try 
      {
         FileConnection fc = (FileConnection)Connector.open("file:///SDCard/test.gif");
         boolean bFileExists = fc.exists();
         if (!bFileExists)
         {
         Dialog.alert("Cannot find specified GIF file.");
         System.exit(0);
         }
         DataInputStream in = fc.openDataInputStream();
         byte[] widthBytes = new byte[2];
         byte[] heightBytes = new byte[2];
		     
         if ( in instanceof Seekable ) 
         {
            ((Seekable) in).setPosition(6);
            in.read(widthBytes,0,2);
		     
            ((Seekable) in).setPosition(8);
            in.read(heightBytes,0,2);
         } 
		     
         int widthPixels  = widthBytes[0]  + 256 * widthBytes[1];
         int heightPixels = heightBytes[0] + 256 * heightBytes[1];
		     
         add(new LabelField("Width: " + widthPixels + "\nHeight: " + heightPixels));
		     
         in.close();
         fc.close();
      }
      catch (IOException ioe) 
      {
         ioe.printStackTrace();
      } 
	} 
}

Code sample: Displaying the path to the video folder using System.getProperty()

import net.rim.device.api.ui.component.LabelField.*;
import net.rim.device.api.ui.container.MainScreen.*;
import net.rim.device.api.ui.UiApplication.*;

public class GetVidDir extends UiApplication
{
    public static void main(String args[])
    {
        GetVidDir app = new GetVidDir();
        app.enterEventDispatcher();
    }
    
    public GetVidDir()
    {
        HomeScreen hs = new HomeScreen();
        pushScreen(hs);
    }
}

class HomeScreen extends MainScreen
{
    public HomeScreen()
    {
        LabelField msg = new LabelField(System.getProperty("fileconn.dir.videos"));
        add(msg);
    }
}

Code sample: Retrieving a list of mounted roots

import java.util.Enumeration.*;
import javax.microedition.io.file.FileSystemRegistry,*;
import net.rim.device.api.ui.component.LabelField.*;
import net.rim.device.api.ui.container.MainScreen.*;
import net.rim.device.api.ui.UiApplication.*;

public class ListMountedRoots extends UiApplication 
{
   public static void main(String[] args) 
   {
      ListMountedRoots app = new ListMountedRoots();
      app.enterEventDispatcher();
   }
	
   public ListMountedRoots()
   {
      pushScreen(new HomeScreen());
   }
}

class HomeScreen extends MainScreen
{
    public HomeScreen() {
        StringBuffer msg = new StringBuffer( “The mounted roots are:\n”);
        Enumeration e = FileSystemRegistry.listRoots();
        while (e.hasMoreElements()) {
            msg.append( e.nextElement() );
            msg.append( ‘\n’ );
        }
        add(new LabelField(msg));
    }

}