Monitoring file system roots

To receive notifications when a file system root is added to or removed from the device:

  1. Implement a FileSystemListener . The mandatory FileSystemListener.rootChanged method is called every time a file system root is either added to or removed from the device.

    The following code snippet creates the framework for a separate listener class that implements FileSystemListener.

    import javax.microedition.io.file.FileSystemListener;
    
    public class MyFileSystemListener implements FileSystemListener {
    
        public void rootChanged(int state, String rootName) {
    
            // Handle detected root changes
            // If state == FileSystemListener.ROOT_ADDED, rootName was added
            // If state == FileSystemListener.ROOT_REMOVED, rootName was removed
    
        }
    
    }
  2. Register the implemented FileSystemListener by calling the FileSystemRegistry.addFileSystemListener method.

    The following code snippet registers a MyFileSystemListener for the MIDlet.

    // Create a MyFileSystemListener instance
    MyFileSystemListener myFileSystemListener = new MyFileSystemListener();
    
    // Register the MyFileSystemListener
    FileSystemRegistry.addFileSystemListener(myFileSystemListener);