Reputation: 1028
im creating a program that will compile java files, at the moment i have the program compiling numerous files at a time in one particular folder. but what i want it to do is to compile all the files in a folder structure given a folder to start (eg. if given the following address C:/files_to_be_compiled
, can you search all the folders within this folder to get a list of all the .class files). I have this code that is getting all the .class
files from a single folder but i need to expand this to get all the .class
files from all the rest of the folders in that folder given
String files;
File folder = new File("C:/files_to_compile");
File[] listOfFiles = folder.listFiles();
{
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
files = listOfFiles[i].getName();
if (files.endsWith(".class") || files.endsWith(".CLASS")) {
System.out.println(files);
}
}
}
}
how would i extend the code above get all the .class files from within all the folders in a given folder?
Upvotes: 0
Views: 2427
Reputation: 21243
You can use DirectoryWalker from Apache Commons to walk through a directory hierarchy and apply a filter - FileFilterUtils.suffixFileFilter(".class")
. For example:
public class ClassFileWalker extends DirectoryWalker {
public ClassFileWalker() {
super(FileFilterUtils.directoryFileFilter(),
FileFilterUtils.suffixFileFilter(".class"), -1);
}
protected void handleFile(File file, int depth, Collection results) {
if(file.isFile())
results.add(file);
}
public List<File> getFiles(String location) {
List<File> files = Lists.newArrayList();
try {
walk(new File(location), files);
} catch (IOException e) {
e.printStackTrace();
}
return files;
}
}
Then use it like this:
ClassFileWalker walker = new ClassFileWalker();
List<File> files = walker.getFiles("C:/files_to_be_compiled");
Upvotes: 0
Reputation: 407
public static void listFilesForFolder(String path)
{
File folder = new File(path);
File[] files = folder.listFiles();
for(File file : files)
{
if (file.isDirectory()){
listFilesForFolder(file.getAbsolutePath());
}
else if (file.isFile())
{
// your code goes here
}
}
}
// run
listFilesForFolder("C:/files_to_compile");
Upvotes: 1
Reputation: 3967
Maybe somwthing like
void analyze(File folder){
String files;
File[] listOfFiles = folder.listFiles();
{
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
files = listOfFiles[i].getName();
if (files.endsWith(".class") || files.endsWith(".CLASS")) {
System.out.println(files);
}
} else if (listOfFiles[i].isDirectory()){
analyze(listOfFiles[i]);
}
}
}
void start(){
File folder = new File("C:/files_to_compile");
analyze(folder);
}
This way, you're analyzing your structure recursively (Depth-first search).
Upvotes: 2
Reputation: 53371
This answer may be of use to you.
Example code borrowed from linked answer:
public void listFilesForFolder(final File folder) {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
System.out.println(fileEntry.getName());
}
}
}
final File folder = new File("/home/you/Desktop");
listFilesForFolder(folder);
Upvotes: 0