Dir listing in Java
import java.io.*;
public class Dir
{
public static void main(String args[])
{
String path = "/home/madan/";
String filter = "*.txt";
FilenameFilter filt = new FilenameFilter() {
public boolean accept(File p, String f) {
String onlyUniverse = ".txt";
boolean b = f.endsWith(onlyUniverse);
return b;
}
};
File dir = new File(path);
String[] children = dir.list(filt);
if (children == null) {
System.out.println(path+ " *ERROR* - cannot access");
} else {
for (int i=0; i<children.length; i++) {
System.out.println(children[i]);
}
}
}
}