J2ME PDA Optional Packages, File Connection
This article will introduce to you what an optional package is and more specifically what is available with the JSR 75 PDA Optional Package with emphasis on the File Connection classes. What is an Optional Package? Optional packages are additional packages vendors may implement along with the J2ME profile. These optional packages are usually added when it makes sense to do so, for example, vendors will only add "Wireless Messaging API" to devices that are capable of sending SMS. However because they are optional and are not apart of the J2ME profile the vendor is not obligated to implement them. In regard to JSR 75 it would best suit devices with access to large amounts of memory storage like the following: * Smart Media card * CompactFlash card (registred trademark of SanDisk Corporation) * Secure Digital card * MultiMedia card * Memory Stick (registered trademark of Sony Corporation) Perfect candidates would be high end mobile devices like SmartPhones. The second reason why vendors would package the JSR 75 into mobile devices is because of the PIM (Personal Information Management) classes such a device would be the Nokia 6630, http://www.admblog.com/blog/2004/06/new_nokia_6630_.html.The PIM classes give you access to additional data contained in the device such as the Calendar, Address Book and ToDo list. For a complete list of all the available optional package visit the Java Community Process website http://www.jcp.org/en/jsr/tech?listBy=1&listByType=platform . Ensure the Package Exists How to detect the package? Use the System.getProperty method with the key "microedition.pim.version" if the version is returned ie: "1.0" then the package exists otherwise if it does not exist a null will be returned. For File Connection use the key "microedition.io.file.FileConnection.version". A second method to determine if the classes exist is to use the Class.forName method: ... public static boolean isFileConnectionExist(){ try { Class.forName( "microedition.io.file.FileConnection.version" ); return true; } catch( Exception e ){ return false; } } ...
Installation and Setup
At the time writing this article Wireless Toolkit version 2.2 Beta from Sun Microsystems includes the JSR 75 PDA Optional Package, visit java.sun.com and dowload the toolkit if you haven't already.. Do not forget to enable JSR 75 in the API selection tab under Settings. That is it! Now you are ready to start using the File Connection classes.

Simple Example
Before you run the example you will need to setup an initial directory structure, under [WTK-HOME\appdb\DefaultColorPhone\filesystem create the following directories * CFCard * MemoryStick * SDCard The above will represents 3 roots / 3 mountable memory areas. These mountable areas represent storage areas on a real device, the names are self-explantory. Under MemoryStick create 3 dummy files and 2 directories. The dummy files will be named as follows: * somepice.pic * somemusic.mp3 * resume.txt * secretfile.txt Now ensure that resume.txt is read only and secretfile.txt is hidden. Create the following directories under MemoryStick directory (nothing are in them): * downloads * recipes Here is what the directory structure should look like:

Source Code:
import java.util.*; import java.io.*; import javax.microedition.io.*; import javax.microedition.io.file.*; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; /** * FileManager - by Jason Lam * * Released under the Public Domain License * */ public class FileManager extends MIDlet { public FileManager() { runSample(); } public void startApp() { } public void pauseApp() { } public void destroyApp(boolean cond) { notifyDestroyed(); } private void runSample() { Enumeration drivesEnum = FileSystemRegistry.listRoots(); System.out.println("\nDrives: "); while (drivesEnum.hasMoreElements()) { String drives = (String)drivesEnum.nextElement(); System.out.println(" " + drives); } try { FileConnection fconn = (FileConnection)Connector.open("file:///MemoryStick/"); Enumeration currentRootEnum = fconn.list("*",true); System.out.println("\nMemoryStick Contents: "); while (currentRootEnum.hasMoreElements()) { String filename = (String)currentRootEnum.nextElement(); fconn = (FileConnection)Connector.open("file:///MemoryStick/" + filename); if (fconn.isDirectory()) System.out.print(" Directory Name: "); else System.out.print(" File Name: "); System.out.print(filename + " Last Modified: " + fconn.lastModified() + " Hidden: " ); if (fconn.isHidden()) System.out.print("Yes"); else System.out.print("No"); System.out.print(" Writeable: "); if (fconn.canWrite()) System.out.print("Yes"); else System.out.print("No"); System.out.println(); } fconn.close(); } catch (IOException ioe) { } } }
Output :

The above example is a fairly simple example but it gives you enough to see how the File Connection classes work. Please refer to the JSR 75 Java Docs for more detail. A couple things you should keep in mind is when executing the delete method you may want to re-open the File Connection to ensure you have the latest directory info otherwise you may throw an exception trying to access something that doesn't exist anymore. Secondly, though most of the methods are self-explanatory the setFileConnection method may not be so obvious because of its traversal functionality. Currently there aren't too many devices packaged with the JSR 75 optional package it won't be long before a lot them will, it was just a little while ago when everyone was complaining "where are the MIDP 2.0 devices?", now there are devices packaged with MIDP 2.0 everywhere It won't be long for more devices enabled with JSR 75, so get started now and build your cool application. Some project ideas are: * File Manager / File Explorer * Interactive Picture Browser * A FTP client with the ability to store files/data on the device itself * A P2P client with the ability to store files/data on the device itself * A better mobile image blogger that has sharing and storage capabilities on the mobile device * A simple editor with the ability to store local files * With the added room games now have the ability to store a lot more data, maps, levels, graphics and sound.
Wireless Toolkit version 2.2 Beta by Sun http://wireless.java.sun.com JSR 75 - http://www.jcp.org/en/jsr/detail?id=75