Path: blob/master/src/java.base/share/classes/java/nio/file/FileVisitor.java
41159 views
/*1* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.file;2627import java.nio.file.attribute.BasicFileAttributes;28import java.io.IOException;2930/**31* A visitor of files. An implementation of this interface is provided to the32* {@link Files#walkFileTree Files.walkFileTree} methods to visit each file in33* a file tree.34*35* <p> <b>Usage Examples:</b>36* Suppose we want to delete a file tree. In that case, each directory should37* be deleted after the entries in the directory are deleted.38* <pre>39* Path start = ...40* Files.walkFileTree(start, new SimpleFileVisitor<Path>() {41* @Override42* public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)43* throws IOException44* {45* Files.delete(file);46* return FileVisitResult.CONTINUE;47* }48* @Override49* public FileVisitResult postVisitDirectory(Path dir, IOException e)50* throws IOException51* {52* if (e == null) {53* Files.delete(dir);54* return FileVisitResult.CONTINUE;55* } else {56* // directory iteration failed57* throw e;58* }59* }60* });61* </pre>62* <p> Furthermore, suppose we want to copy a file tree to a target location.63* In that case, symbolic links should be followed and the target directory64* should be created before the entries in the directory are copied.65* <pre>66* final Path source = ...67* final Path target = ...68*69* Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,70* new SimpleFileVisitor<Path>() {71* @Override72* public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)73* throws IOException74* {75* Path targetdir = target.resolve(source.relativize(dir));76* try {77* Files.copy(dir, targetdir);78* } catch (FileAlreadyExistsException e) {79* if (!Files.isDirectory(targetdir))80* throw e;81* }82* return CONTINUE;83* }84* @Override85* public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)86* throws IOException87* {88* Files.copy(file, target.resolve(source.relativize(file)));89* return CONTINUE;90* }91* });92* </pre>93*94* @since 1.795*/9697public interface FileVisitor<T> {9899/**100* Invoked for a directory before entries in the directory are visited.101*102* <p> If this method returns {@link FileVisitResult#CONTINUE CONTINUE},103* then entries in the directory are visited. If this method returns {@link104* FileVisitResult#SKIP_SUBTREE SKIP_SUBTREE} or {@link105* FileVisitResult#SKIP_SIBLINGS SKIP_SIBLINGS} then entries in the106* directory (and any descendants) will not be visited.107*108* @param dir109* a reference to the directory110* @param attrs111* the directory's basic attributes112*113* @return the visit result114*115* @throws IOException116* if an I/O error occurs117*/118FileVisitResult preVisitDirectory(T dir, BasicFileAttributes attrs)119throws IOException;120121/**122* Invoked for a file in a directory.123*124* @param file125* a reference to the file126* @param attrs127* the file's basic attributes128*129* @return the visit result130*131* @throws IOException132* if an I/O error occurs133*/134FileVisitResult visitFile(T file, BasicFileAttributes attrs)135throws IOException;136137/**138* Invoked for a file that could not be visited. This method is invoked139* if the file's attributes could not be read, the file is a directory140* that could not be opened, and other reasons.141*142* @param file143* a reference to the file144* @param exc145* the I/O exception that prevented the file from being visited146*147* @return the visit result148*149* @throws IOException150* if an I/O error occurs151*/152FileVisitResult visitFileFailed(T file, IOException exc)153throws IOException;154155/**156* Invoked for a directory after entries in the directory, and all of their157* descendants, have been visited. This method is also invoked when iteration158* of the directory completes prematurely (by a {@link #visitFile visitFile}159* method returning {@link FileVisitResult#SKIP_SIBLINGS SKIP_SIBLINGS},160* or an I/O error when iterating over the directory).161*162* @param dir163* a reference to the directory164* @param exc165* {@code null} if the iteration of the directory completes without166* an error; otherwise the I/O exception that caused the iteration167* of the directory to complete prematurely168*169* @return the visit result170*171* @throws IOException172* if an I/O error occurs173*/174FileVisitResult postVisitDirectory(T dir, IOException exc)175throws IOException;176}177178179