Professional Documents
Culture Documents
By : Eyob Gebretinsae
1
Beyond the typically small persistent storage managed by MIDP’s
record store mechanism, devices may have additional persistent
data storage mechanisms that can be made available to Java ME
applications.
Large file systems can be found on devices that accept plug-in flash
memory cards.
The File Connection Optional Package provides an API to access
these file systems.
The record store mechanism of MIDP is inefficient for handling
large-capacity storage.
The persistent storage on these cards is accessed as a file system,
instead of a database, with directories and files.
2
This optional API is contained in the
javax.microedition.io.file package.
This optional package is implemented on top of CLDC.
It is always a good idea to make sure an optional API is
code is compatible.
Null is returned if the API is not available.
3
You perform read and write operations through the input
and output stream classes from the java.io package.
To obtain a stream to a file in the file system, you first need
API.
The main interface of this API is the FileConnection
interface.
You obtain an instance of a FileConnection by using a
Framework(GCF) in CLDC.
GCF is used primarily to make connections through a
The mode indicates the type of access you want for the file
connection.
You can use Connector.READ, Connector.WRITE, or
Connector.READ_WRITE.
5
E.g, you may get an input stream to an existing abc.txt file on an
SD card using the following:
FileConnection fc = (FileConnection)
Connector.open("file:/// SDCard
/abc.txt",Connector.READ);
InputStream is = fc.openInputStream();
You can then use the InputStream for reading the file.
The FileConnection interface has five methods for obtaining a
stream:
DataInputStream openDataInputStream()
DataOutputStream openDataOutputStream()
InputStream openInputStream()
OutputStream openOutputStream()
OutputStream openOutputStream(long Offset)
6
The second variation of openOutputStream() allows you to
specify a byte offset to start writing to the file.
File or Directory
An open FileConnection can be referring to either a directory
or a file.
You can determine if the connection is associated with a
8
Some file attributes may prevent you from reading or
writing to a file. You can determine whether a file can be
read by using this method:
public boolean canRead()
Or find out if a file can be written to using the following:
9
Your application may need to determine the available space
on a file system associated with a FileConnection instance.
public long availableSize()
This returns the total available space in bytes in the entire file
usedSize(), you can figure out the total size of the file system.
10
If you only want to find out the size of the specific file
associated with the current FileConnection instance, use
the following method:
public long fileSize()
If the FileConnection instance refers to a directory, you
can find out the total size of all the files in the directory
by calling the following method:
public long directorySize()
11
To create a new file, you first have to use Connector.open()
with the new file name and Connector.WRITE mode.
A FileConnection will be returned, but the file does not yet
exist.
You can confirm its nonexistence by calling
javax.microedition.io.file.FileSystemRegistry
class.
16
You can add and remove file system listeners by using the
two static methods of FileSystemRegistry:
rootName)
This method is called whenever there is a change in the
18
Personal Information Management (PIM) are databases
Many devices, especially phones, have the ability to
21
A PIM list can be obtained using one of these
openPIMList() methods:
public PIMList openPIMList(int
PIM.EVENT_LIST, or PIM.TODO_LIST.
The access mode can be PIM.READ_ONLY,
PIM.READ_WRITE, or PIM.WRITE_ONLY.
22
Once you have a list opened, you can start accessing its
content.
The openPIMList() call returns a PIMList. The
(ContactList)pimInst.openPIMList( PIM.C
ONTACT_LIST, PIM.READ_WRITE);
23
All the records in a list can be accessed via an
Enumeration (java.util.Enmeration) of PIMItems.
You can obtain the Enumeration using one of the
PIMException
public Enumeration items(PIMItem
24
You can classify a list of items by categories.
A category is a just a string. E.g, a “personal” category can be
used to flag all the personal contacts in the content list, while a
“business” category is used for business contacts.
To obtain all the list items in a particular category, you can use
the following:
public Enumeration itemsByCategory (String
category) throws PIMException
To get a list of categories supported by the device, call
String [] getCategories() throws
PIMException
If you get a zero-length array in return, you know that either the
device does not support categories or the user hasn’t defined
any.
25
Once you’ve determined that the device supports categories,
you can add new categories using the following:
public void addCategory(String category)
throws PIMException
This method will never add the same category twice. It will
26
Set the deleteUnassignedItems flag to true if you want
these items deleted from the list.
The deleteCategory() method will return without throwing
27