![]() Within this method you may decide whether to ignore the error, handle it somehow or to explicitly throw the exception which will in turn terminate the traversal. permission denied error for a file or directory), no exception is thrown but instead method visitFileFailed from FileVisitor interface is called with the reference to that exception. When an error occurs during the traversal (e.g. ![]() In a typical case CONTINUE should be used when traversing. TERMINATE – Specifies that the traversal should be aborted immediately without invoking any other callbacks.SKIP_SIBLINGS – Specifies that siblings of the current file or directory should be skipped.This value makes sense only for preVisitDirectory SKIP_SUBTREE – Specifies that given directory and its contents should not be traversed (they should be skipped).CONTINUE – Specifies that the traversal should continue normally.Controlling visiting processĮach method of FileVisitor returns FileVisitResult enumeration value which indicates whether and how to continue the traversal. It’s worth noting that the first method does not follow symbolic links and does not limit the depth of the traversal. At the moment there is only one available option FOLLOW_LINKS which indicates whether the traversal follows symbolic links. The second walkFileTree method gives more control by allowing to specify special options and maximum depth of the recursive traversal. visitFileFailed – Called when some error occurred when visiting given file or directory.visitFile – Called when visiting the file.postVisitDirectory – Called when leaving a directory (after all entries of a directory were visited including its subdirectories).preVisitDirectory – Called when entering a directory (before any entry of a directory is visited).Actually, there are four possible events and they relate to the following methods of FileVisitor interface: The first method begins recursive (depth-first) traversal process starting from the path specified as the first argument and invokes appropriate methods on visitor when some events occur. Path walkFileTree(Path start, Set options, int maxDepth, FileVisitor visitor) Path walkFileTree(Path start, FileVisitor visitor) When you want to recursively visit specific directory, you call one of the following methods from Files class: ![]() The good thing is that Java 7 introduced a very convenient way of doing it using FileVisitor. The task is not very difficult but doing it manually is a bit cumbersome. The typical use case is searching files of a given type, copying or removing a directory including all its contents. Sometimes you may need to recursively visit all files and subdirectories of given directory and perform some actions on it.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |