Java Reference
In-Depth Information
The
addFileSystemListener
method may throw one of the following exceptions:
•
SecurityException
: If your application is not permitted to listen for file system
changes
•
NullPointerExecption
: If you specify a nonexistent listener
Similarly, the
removeFileSystemListener
method throws a
NullPointerException
if you
invoke it with
null
.
■
Tip
When working in NetBeans, you can simulate the addition and removal of file system roots using the
emulator menu. Choose MIDlet
➤
External Events.
Another common use of the
FileSystemRegistry
is to determine which roots are
currently mounted. Odds are that you'll want to do this before you first invoke an FCOP
class's interface, to help ensure your application is portable between devices. Some
devices, like the emulator, ensure that there's a default file system named
root1
, but
there's no guarantee that all devices have the same root file names. You can list the root
file systems on a device using the
FileSystemRegistry.listRoots
method, which returns
an
Enumeration
of
String
items, each a single root file system name. For example, you
might write something like what's shown in Listing 7-3.
Listing 7-3.
Enumerating Root File Systems on the Device
Enumeration r = FileSystemRegistry.listRoots();
String cr = null;
while (r.hasMoreElements()) {
cr = (String) roots.nextElement();
/* do something with the discovered root */
}
Be advised that like the other
FileSystemRegistry
methods,
listRoots
can throw a
SecurityException
if your application isn't permitted to enumerate the root file systems
on a device.
Putting the FCOP to Work
Listing 7-4 shows the
LocationStore
class I presented in the previous chapter for storing
weather forecasts refactored to use a file instead of the record store.