Sevy
Sevy

Reputation: 698

Walking file tree in Java 6

I'm trying to find a way to locate a certain file in the computer's directory in java 6. More specifically, my program launches the program Pymol by locating pymol.exe on the hard drive and executing a command to launch it. I have it working fine in Java 7, by using the File Visitor interface. However, I'm trying to make it compatible with Java 6, so I need to find an alternate method for locating this program (or identifying that the user does not have it installed). Any ideas?

Upvotes: 1

Views: 1684

Answers (2)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136002

try

File findFile(String name, File dir) {
    for (File f : dir.listFiles()) {
        if (f.isFile() && f.getName().equals(name)) {
            return f;
        }
    }
    for (File f : dir.listFiles()) {
        if (f.isDirectory()) {
            return findFile(name, f);
        }
    }
    return null;
}

Upvotes: 2

Cratylus
Cratylus

Reputation: 54074

Just do a recursive tree traversal in your file directory until you either find the file or the traversal ends.

Upvotes: 0

Related Questions