0

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?

newSpringer
  • 1,018
  • 10
  • 28
  • 44
  • exact duplicate of [Read all files in a folder](http://stackoverflow.com/questions/1844688/read-all-files-in-a-folder) – Alnitak May 14 '12 at 15:09
  • You should use recursion to traverse all directories within the current directory to scan for .class files.. also, use .toLowerCase() instead of comparing .class AND .CLASS – Tyler May 14 '12 at 15:21

4 Answers4

2

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).

StepTNT
  • 3,867
  • 7
  • 41
  • 82
  • 3
    To prevent extensions like .Class or .cLaSs from slipping through, you might want to do a check like `filename.toUppercase().endsWith(".CLASS")` – Mark Jeronimus May 14 '12 at 15:13
  • I know, but this is his code, I've just added the `else if` part :) Good point though! – StepTNT May 14 '12 at 15:14
  • @Zom-B: It isn't a good point which you will recognize, if you put your ClAsS-files into a jar, and try to find them there, maybe on different plattforms. I've never seen a compiler not producing lowercase, and if there is, it should fail early, to be fixed. – user unknown May 14 '12 at 15:24
  • `filename.toUppercase()` doesn't mean that the compiler will produce only uppercase files, it's just used to check the file extension without taking care of the string format. And a `.ClAsS` file is not valid in Java. (Assuming that I understood what you're saying!) – StepTNT May 14 '12 at 15:30
  • @StepTNT: when you say You just need to use a method to call analyze(starting-folder-here), do you create a new file (File folder2 = new File("files_to_compile");) and put that into analyze(folder2)? – newSpringer May 14 '12 at 15:36
  • @StepTNT: thanks, ya was thinking that was the way to do it alright – newSpringer May 14 '12 at 16:03
1
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");
Mustafa
  • 401
  • 2
  • 7
0

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);
Community
  • 1
  • 1
Elliot Bonneville
  • 51,872
  • 23
  • 96
  • 123
  • 2
    you should vote to close the question, not just plagiarise the other question's answer. – Alnitak May 14 '12 at 15:10
  • @Alnitak: Plagiarizing consists of "stealing" text from another source without referencing the source. By that definition I am not plagiarizing the other answer. – Elliot Bonneville May 14 '12 at 15:11
  • ok, perhaps that was a little strong, but the point still stands - you identified an identical question, the standard procedure is to vote to close. – Alnitak May 14 '12 at 15:16
  • @Alnitak: what Elliot says is true and it answered my question as well so it should actually be marked up not down – newSpringer May 14 '12 at 15:19
  • @newSpringer I didn't mark it down. I didn't vote it up either, though - IMHO getting up votes requires work. – Alnitak May 14 '12 at 15:49
0

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");
tenorsax
  • 21,123
  • 9
  • 60
  • 107