read files from directory and filter files from Java
- by Adnan
The following codes goes through all directories and sub-directories and outputs just .java files;
import java.io.File;
public class DirectoryReader {
private static String extension = "none";
private static String fileName;
public static void main(String[] args ){
String dir = "C:/tmp";
File aFile = new File(dir);
ReadDirectory(aFile);
}
private static void ReadDirectory(File aFile) {
File[] listOfFiles = aFile.listFiles();
if (aFile.isDirectory()) {
listOfFiles = aFile.listFiles();
if(listOfFiles!=null) {
for(int i=0; i < listOfFiles.length; i++ ) {
if (listOfFiles[i].isFile()) {
fileName = listOfFiles[i].toString();
int dotPos = fileName.lastIndexOf(".");
if (dotPos > 0) {
extension = fileName.substring(dotPos);
}
if (extension.equals(".java")) {
System.out.println("FILE:" + listOfFiles[i] );
}
}
if(listOfFiles[i].isDirectory()) {
ReadDirectory(listOfFiles[i]);
}
}
}
}
}
}
Is this efficient? What could be done to increase the speed?
All ideas are welcome.