Clear folder - delete files in folder - J2ME
I am trying to c开发者_开发问答lear all the files in a folder using j2me. How do I do that?
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 :-)
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.
精彩评论