Search code examples
javajava-mejsr75

Clear folder - delete files in folder - J2ME


I am trying to clear all the files in a folder using j2me. How do I do that?


Solution

  • Since you are using J2ME, the java.io.File class is not available to you.

    So I am assuming that you are using the FileConnector Optional Package (FCOP).

    Take a look at the javadocs for javax.microedition.io.file.FileConnection, and you should be able to figure out the details.

    I'm not a J2ME expert, but I think that the code would look something like this:

    FileConnection fconn = (FileConnection) Connector.open("file:///SomeDirectory");
    Enumeration en = fconn.list();
    while (en.hasMoreElements()) {
        String name = en.nextElement();
        FileConnection tmp = (FileConnection) Connector.open(
            "file:///SomeDirectory/" + name);
        tmp.delete();
        tmp.close();
    }
    

    Exception handling, proper resource handling (using finally) is left as an exercise for the reader :-)