Java Reference
In-Depth Information
Opening a File
Although you've opened a
FileConnection
referring to the file you want to open, you still
need to open the file itself to obtain an input or output stream. You do this using one of
the following methods:
•
openDataInputStream
: Returns a
DataInputStream
from which you can read the file's
contents
•
openInputStream
: Returns an
InputStream
from which you can read the file's contents
•
openDataOutputStream
: Returns a
DataOutputStream
to which you can write data to
the file
•
openOutputStream
: Returns an
OutputStream
to which you can write data to the file
Of course, you can only obtain the streams that correspond to the modes in which you
opened the
FileConnection
; for example, you couldn't obtain an
OutputStream
if you speci-
fied
Connector.READ
to
Connector.open
. Mismatching these will cause the runtime to throw
an
IllegalModeException
. You should be prepared to handle the
SecurityException
and
IOException
exceptions as well.
What if you want to write starting somewhere past the first byte of the file? To do
this, simply invoke
openOutputStream
with the offset at which you want to begin writing.
Be sure your offset is positive, or the runtime will throw an
IllegalArgumentException
your way.
Tweaking File Attributes
The
FileConnection
interface has a host of methods that let you query or tweak attributes
of the file or directory to which your
FileConnection
is associated, including the following:
•
canRead
: Returns
true
if your application is permitted to write to the file
•
canWrite
: Returns
true
if your application is permitted to write to the file
•
directorySize
: Returns the number of bytes the directory consumes; you can pass
true
to include the directory's subdirectories in the computation, or
false
to only
include the current directory
•
fileSize
: Returns the number of bytes the file consumes
•
getName
: Returns the name of the file, excluding the GCF URL protocol
•
getPath
: Returns the name of the directory containing the file, excluding the GCF
URL protocol