Java Reference
In-Depth Information
The GCF, which you'll learn about in detail in Chapter 12, provides a unified class
hierarchy and means to access local and remote connection resources using URLs and
a stream-based interface. The javax.microedition.io.Connector class operates as a fac-
tory of connections for your application; you pass a URL describing the item you want
to open, and the Connector class returns an instance of a Connection subclass such as
FileConnection . With the FileConnection in hand, you can perform I/O using streams,
and you can access the particulars of the file system. You can create new files and direc-
tories as well as remove existing files and directories.
A URL referencing a local file on the file system uses the file:// protocol prefix
instead of the http:// prefix, like this:
FileConnection fc =
(FileConnection) Connector.open("file:///SDC/wx.xml", Connector.READ);
Why are there three solidus ( / ) characters? The first two separate the file protocol
indicator from the path to the file. The region between the second and the third is
reserved in URLs for the host name of the object being manipulated; in this case, because
the host name is the local host, it's empty.
Caution It's a common mistake to type out the URL for a file the same way you would a URL for a web
resource, using only two slashes. It's an error, however, and one that's hard to catch except by close inspection.
You name files and directories the same way; the URL file:///SDC/directory may
either be a file or a directory, and it's up to you and the APIs to know the difference.
That's important when manipulating directories; as you'll see in a moment, opening a
FileConnection to file:///SDC/directory/ and invoking the FileConnection.mkdir
method to create the directory is an error.
Using the FCOP
The sequence of events when using the FCOP goes something like this:
1. Determine whether or not the device running your application has the FCOP.
2. From the Connector class, obtain a FileConnection instance representing the file
you want to access.
3. Create the file if necessary.
4. Open the file for reading or writing and get an input or output stream,
respectively.
 
Search WWH ::




Custom Search