How to list files and folders in Java ME
Article Metadata
Overview
This article shows how to list files and folders in a folder on a mobile device. FileConnection API (JSR-75) has FileConnection.list() method for this purpose. It is also possible to get directory and file sizes by using FileConnection.directorySize() and FileConnection.fileSize() methods.
The full source code for a test MIDlet:
Source code: FileListMIDlet.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Enumeration;
import javax.microedition.io.file.*;
import javax.microedition.io.*;
import java.io.*;
public class FileListMIDlet extends MIDlet implements CommandListener {
private Form form;
private Command exitCommand;
public void startApp() {
form = new Form("C:/ contents"); //creates a new Form screen
exitCommand = new Command("Exit", Command.EXIT, 1); //creates the exit command
form.addCommand(exitCommand); //adds the command to the Form
form.setCommandListener(this); //sets a command listener
Display.getDisplay(this).setCurrent(form);
getFileList("file:///c:/");
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
protected void getFileList(String path) {
try {
//Opens a file connection in READ mode
FileConnection fc = (FileConnection)Connector.open(path, Connector.READ);
Enumeration filelist = fc.list("*", true); //also hidden files are shown
String filename;
while(filelist.hasMoreElements()) {
filename = (String) filelist.nextElement();
fc = (FileConnection)Connector.open(path + filename, Connector.READ);
if(fc.isDirectory()) { //checks if fc is a directory
long size = fc.directorySize(false);
form.append(filename+" - "+Integer.toString((int)size)+"B\n");
} else { //otherwise, is a file
long size = fc.fileSize();
form.append(filename+" - "+Integer.toString((int)size)+"B\n");
}
}
fc.close();
}
catch (IOException ioe) {
System.out.println("IOException: "+ioe.getMessage());
}
catch (SecurityException se) {
System.out.println("SecurityException: "+se.getMessage());
}
}
public void commandAction(Command c, Displayable d) {
if (c == exitCommand) this.notifyDestroyed();
}
}


29 Sep
2009
This article demonstrates basic use of the PIM (JSR-75) API. It shows how we can list the files and folders available in a certain file system root using this API. This is a fairly basic article, and mainly consists of a code example, showing how to retrieve a list of all the files and folders in the c:/ file system root (usually the phone's built-in memory). The code example shows how to use a FileConnection object and its "list" method to retrieve an enumeration containing all the files and folders at a particular file path. The code also shows how to navigate through this enumeration, how to distinguish between files and folders, and how to find out file and folder size.
The article is very simple, but the code example does a good job of demonstrating several fundamental concepts that that File Connection API is commonly used for. This article is therefore useful reading for those who are new to the API, or who just want to find out how to get a list of files and folders using the API.