Java Reference
In-Depth Information
FileConnection fc =
(FileConnection) Connector.open("file:///wx.xml", Connector.READ);
This method takes two arguments: the URL to the file you wish to open, and the
mode in which it should be opened. The mode is one of
Connector.READ
,
Connector.WRITE
,
or
Connector.READ_WRITE
.
Not surprisingly, the
open
method can throw exceptions that you must handle,
including the following:
•
IllegalArgumentException
: If a parameter is invalid
•
ConnectionNotFoundException
: If the target of the name cannot be found, or if the
requested protocol type is not supported
•
IOException
: If some other kind of I/O error occurs
•
SecurityException
: If access to the protocol handler is prohibited
Creating a New File or Directory
Three
FileConnection
interfaces exist to help you manage the creation of files and
directories:
•
exists
: Returns a
boolean
indicating whether or not the file name indicated when
the
FileConnection
was created exists
•
create
: Creates the named file, assuming you opened it using the
Connector
in
Connector.WRITE
or
Connector.READ_WRITE
mode
•
mkdir
: Creates the named directory, assuming you opened it using the
Connector
in
Connector.WRITE
or
Connector.READ_WRITE
mode
It's important to realize, then, that simply passing a file name to
Connector.open
and invoking it with
Connector.WRITE
or
Connector.READ_WRITE
doesn't
actually create
the file for you!
The creation interfaces are void interfaces, but they can throw
IOException
,
SecurityException
,
IllegalModeException
, or
ConnectionClosedException
;
exists
can throw
SecurityException
,
IllegalModeException
, or
ConnectionClosedException
.
■
Caution
You cannot create a directory by specifying a trailing solidus (
/
) on the URL for a file and then
calling
create
; the trailing solidus will cause
create
to throw an
IOException
. Use
mkdir
instead with a
path that does not have a trailing solidus.