To access and perform operations on an existing file or directory in the device file system:
Open a FileConnection
to the file or directory by
calling the Connector.open
method with the complete URL
of the file or directory. The URL is a string with the format file://<host>/<path>
as defined in RFC 1738 and RFC 2396. The host
is normally left empty
and the path
contains the full path to the file or
directory.
Note: The Connector.open
method does not check whether the file or directory specified by
the URL exists, so you can open a connection to a nonexistent file
or directory.
You can specify the URL in one of the following ways:
Use an FC API system property to retrieve the URL of a default storage directory and extend it with your own path, for example:
String url = System.getProperty("fileconn.dir.music") + "MyMusic/song.mp3";
It is recommended that you use this solution whenever possible, since it is likely to work on the widest range of devices.
Use a virtual root and directory and extend them with your own path, on Nokia Asha software platform devices, for example:
String url = "file:///" + "Phone/" + "_my_music/" + "song.mp3";
Use the full logical directory and file path, on Series 40 devices, for example:
String url = "file:///C:/predefgallery/predefmusic/song.mp3";
Note: Do not mix logical and virtual roots and directories in the same URL. If you mix them, the Java Runtime throws an exception.
The following code snippet opens a FileConnection
to the file MyMusic/song.mp3
located in the default
directory for storing music files.
String url = System.getProperty("fileconn.dir.music") + "MyMusic/song.mp3"; FileConnection fc; try { fc = (FileConnection)Connector.open(url); // use the fc object to perform operations on the file (see steps 2-4 of this example) } catch (IllegalArgumentException iae) { } catch (ConnectionNotFoundException cnfe) { } catch (IOException ioe) { } catch (SecurityException se) { } // catch any other exceptions
Check that the
file or directory exists by calling the exists
method on the FileConnection
instance. If you try to perform an operation on a nonexistent file
or directory, the Java Runtime throws an exception.
if (!fc.exists()) { // If the file or directory does not exist, // program the MIDlet to behave accordingly }
Perform the
required operations on the file or directory by using the methods
provided by the FileConnection
interface.
If you write data to an output stream of the FileConnection
instance (either an OutputStream
or a DataOutputStream
) and want the data to be
immediately committed, but do not want to close the output stream
itself, call the output stream's flush
method.
The following code snippet checks that the target file is writable,
opens a DataOutputStream
to the file, writes data
to the DataOutputStream
, and then immediately commits
the changes by calling the DataOutputStream.flush
method. When the DataOutputStream
is no longer
needed, it is closed. Closing the DataOutputStream
also commits any remaining changes.
if (!fc.canWrite()) { // If the file is not writable, // abort the writing process } // Open a DataOutputStream to the file DataOutputStream dos = fc.openDataOutputStream(); // Write data to the DataOutputStream // Commit the data to file dos.flush(); // Write more data to the DataOutputStream // Close the DataOutputStream and commit any data not yet flushed dos.close()
Close the FileConnection
by calling its close
method.
fc.close();