John Ormerod
John Ormerod

Reputation: 117

I would like to useJava 7's FileVisitor to walk up a tree

I have looked and searched for some guidance on how to start at a low point in a path and walk up (or 'back'), until I find a folder with the name I am searching for. The FileVisitor class looks like it ought to be able to help me, but it only seems to work from head to toe.

Is there something that someone could point me to?

Thanks, John

{edited: I seem to be discouraged from saying thanks to the two people who replied in a comment. So thanks! I had a 'duh!' moment when I saw the simple approach. And the article looks useful to someone starting to use FileVisitor. Put them together, and I could go up and then down, if I needed to. John]

Upvotes: 0

Views: 84

Answers (2)

To walk up a tree the easiest way is to iterate using File.getParentFile():

folder = startFromFile;
while (folder != null && !nameToFind.equals(folder.getName())) {
    folder = folder.getParentFile();
}

Upvotes: 1

Dark Knight
Dark Knight

Reputation: 8357

This post about walking file tree might help you.

Upvotes: 0

Related Questions