Creating a new file or directory

To create a new file or directory in the device file system:

  1. Open a FileConnection to the file or directory by calling the Connector.open method with the complete URL of the file or directory to be created. For more information about specifying the URL, see section Accessing files and directories.

  2. Create the file or directory by calling the create or mkdir method on the FileConnection instance.

    The following code snippet opens a FileConnection to the <DefaultMusicDirectory>/MyMusic subdirectory and creates it, provided it does not already exist.

    String url = System.getProperty("fileconn.dir.music") + "MyMusic";
    FileConnection fc;
    try {
        fc = (FileConnection)Connector.open(url);
        if (!fc.exists()) {
            // If the directory does not exist,
            // proceed with the creation
            fc.mkdir();
        }
    } catch (IllegalArgumentException iae) {
    } catch (ConnectionNotFoundException cnfe) {
    } catch (IOException ioe) {
    } catch (SecurityException se) {
    } catch (ConnectionClosedException cce) {
    } catch (IllegalModeException ime) {
    }

The file or directory has been created. You can now either close the connection to it or perform operations on it.