Clear folder - delete files in folder - J2ME

2.6k views Asked by At

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

2

There are 2 answers

2
Stephen C On BEST ANSWER

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 :-)

2
Lawrence Dol On

Use File.list() or File.listFiles() to get a list of the files. Then iterate the list and use File.delete() to delete them. The use File.delete() to delete the directory.

If you want to include subdirectories, do the previous code recursively, recursing as you hit each subdirectory, before you delete the directory.