View RSS Feed

My Java Tips

Jsr 75

Rate this Entry
by , 11-22-2011 at 05:52 PM (1431 Views)
JSR 75 specifies 2 optional packages for mobile devices. I will be talking about these in the next few posts.


The two provided packages are:
Java Code:
javax.microedition.io.file
javax.microedition.pim
PIM stands for personal information and it contains following information:
- contact list
- todo list
- events

If you want to access these from a J2ME application, then javax.microedition.pim is to be used.

Mobile devices have a file system which contains files. You might be interested in fetching those and playing with them. In that case, javax.microedition.io.file provides you with all the needed functionalities.

Interfaces:

FileConnection // Interface for access to files or directories.
FileSystemListener // Listener interface for receiving status notification when adding or removing a file-system root.

Classes:

FileSystemRegistry // Central registry for listeners that need to add or remove a file system.
ConnectionClosedException // Exception thrown when a method of a file connection is invoked but cannot be completed because the connection is closed.
IllegalModeException // Exception thrown when a method is invoked that requires a particular security mode, such as READ or WRITE, but the connection opened is not in that mode.

Let me present an example. I will create/open a file from the file system using FileConnection class of javax.microedition.io.file package.
Java Code:
public void createFile() {
   try {
      FileConnection filecon = (FileConnection)
         Connector.open("file:///SDCard/mynewfile.txt");
      // Always check whether the file or directory exists.
      // Create the file if it doesn't exist.
      if(!filecon.exists()) {
         filecon.create();
      }
      filecon.close();
   } catch(IOException ioe) {
   }
}
As you know already, we use JSR 75 optional package (javax.microedition.io.file) to access the files on the mobile device.

After getting the FileConnection, we will use an inputstream to read the contents into a byte array. Once we have all the contents int he byte array, we will display the contents on the console after converting them into String.

Java Code:
public void showFile(String fileName) {
   try {
      FileConnection fc = (FileConnection)
         Connector.open("file:///CFCard/" + fileName);
      if(!fc.exists()) {
         throw new IOException("File does not exist");
      }
      InputStream is = fc.openInputStream();
      byte b[] = new byte[1024];
      int length = is.read(b, 0, 1024);
      System.out.println
         ("Content of "+fileName + ": "+ new String(b, 0, length));
   } catch (Exception e) {
   }
}

Submit "Jsr 75" to Facebook Submit "Jsr 75" to Digg Submit "Jsr 75" to del.icio.us Submit "Jsr 75" to StumbleUpon Submit "Jsr 75" to Google

Tags: j2me, jsr 75, pim Add / Edit Tags
Categories
Java ME

Comments

  1. henna's Avatar
    • |
    • permalink
    i want to read a text file, where should i be saving it..