I need to do something with the file visited last in a directory. How can I know if the current call to my visitFile() is the last one?
(I only want to list all the files and directories in a given directory. To do so, I’ve introduced a depth field to my FileVisitor implementation and in the preVisitDirectory I return SKIP_SUBTREE if the depth is greater than 0. (And then increment the depth.) The problem is that I don’t know when to reset the depth to 0, because when I call the walkFileTree with this FileVisitor implementation for another directory, the depth is already > 0 and it only lists the given directory.)
How about maintaining the depth only within the two methods,
preVisitDirectoryandpostVisitDirectory? You’ll incrementdepthinpreVisitDirectoryand decrement it inpostVisitDirectory. You might have to initializedepthto-1, to havedepth == 0when in the start directory though. That way, you’ll always have the rightdepth.Edit: If you return
SKIP_SIBLINGSfromvisitFile, instead of frompreVisitDirectory, thepostVisitDirectorywill still get called!Here’s a code sample:
}