read files from directory and filter files from Java
Posted
by Adnan
on Stack Overflow
See other posts from Stack Overflow
or by Adnan
Published on 2010-06-10T09:17:43Z
Indexed on
2010/06/10
9:23 UTC
Read the original article
Hit count: 209
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.
© Stack Overflow or respective owner