-
Notifications
You must be signed in to change notification settings - Fork 668
/
FileVisitor.java
166 lines (158 loc) · 6.65 KB
/
FileVisitor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.nio.file;
import java.io.IOException;
import java.nio.file.attribute.BasicFileAttributes;
/**
* A visitor of files. An implementation of this interface is provided to the
* {@link Files#walkFileTree Files.walkFileTree} methods to visit each file in
* a file tree.
*
* <p> <b>Usage Examples:</b>
* Suppose we want to delete a file tree. In that case, each directory should
* be deleted after the entries in the directory are deleted.
* <pre>
* Path start = ...
* Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
* @Override
* public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
* throws IOException
* {
* Files.delete(file);
* return FileVisitResult.CONTINUE;
* }
* @Override
* public FileVisitResult postVisitDirectory(Path dir, IOException e)
* throws IOException
* {
* if (e == null) {
* Files.delete(dir);
* return FileVisitResult.CONTINUE;
* } else {
* // directory iteration failed
* throw e;
* }
* }
* });
* </pre>
* <p> Furthermore, suppose we want to copy a file tree to a target location.
* In that case, symbolic links should be followed and the target directory
* should be created before the entries in the directory are copied.
* <pre>
* final Path source = ...
* final Path target = ...
*
* Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
* new SimpleFileVisitor<Path>() {
* @Override
* public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
* throws IOException
* {
* Path targetdir = target.resolve(source.relativize(dir));
* try {
* Files.copy(dir, targetdir);
* } catch (FileAlreadyExistsException e) {
* if (!Files.isDirectory(targetdir))
* throw e;
* }
* return CONTINUE;
* }
* @Override
* public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
* throws IOException
* {
* Files.copy(file, target.resolve(source.relativize(file)));
* return CONTINUE;
* }
* });
* </pre>
*
* @since 1.7
*/
// 文件树遍历器,包含了遍历过程中对各种遍历事件的回调处理
public interface FileVisitor<T> {
/**
* Invoked for a directory before entries in the directory are visited.
*
* <p> If this method returns {@link FileVisitResult#CONTINUE CONTINUE},
* then entries in the directory are visited. If this method returns {@link
* FileVisitResult#SKIP_SUBTREE SKIP_SUBTREE} or {@link
* FileVisitResult#SKIP_SIBLINGS SKIP_SIBLINGS} then entries in the
* directory (and any descendants) will not be visited.
*
* @param dir a reference to the directory
* @param attrs the directory's basic attributes
*
* @return the visit result
*
* @throws IOException if an I/O error occurs
*/
// 【进入目录】遍历文件树过程中遇到了目录,attrs是目录的属性
FileVisitResult preVisitDirectory(T dir, BasicFileAttributes attrs) throws IOException;
/**
* Invoked for a file in a directory.
*
* @param file a reference to the file
* @param attrs the file's basic attributes
*
* @return the visit result
*
* @throws IOException if an I/O error occurs
*/
// 【遇到文件】遍历文件树过程中遇到了文件(而不是目录),或者递归层次达到了上限,attrs是文件/目录的属性
FileVisitResult visitFile(T file, BasicFileAttributes attrs) throws IOException;
/**
* Invoked for a file that could not be visited. This method is invoked
* if the file's attributes could not be read, the file is a directory
* that could not be opened, and other reasons.
*
* @param file a reference to the file
* @param exc the I/O exception that prevented the file from being visited
*
* @return the visit result
*
* @throws IOException if an I/O error occurs
*/
// 【遇到异常】遍历文件树过程中遇到了异常,ex是异常信息
FileVisitResult visitFileFailed(T file, IOException ex) throws IOException;
/**
* Invoked for a directory after entries in the directory, and all of their
* descendants, have been visited. This method is also invoked when iteration
* of the directory completes prematurely (by a {@link #visitFile visitFile}
* method returning {@link FileVisitResult#SKIP_SIBLINGS SKIP_SIBLINGS},
* or an I/O error when iterating over the directory).
*
* @param dir a reference to the directory
* @param exc {@code null} if the iteration of the directory completes without
* an error; otherwise the I/O exception that caused the iteration
* of the directory to complete prematurely
*
* @return the visit result
*
* @throws IOException if an I/O error occurs
*/
// 【退出目录】结束了对指定目录的遍历,ex是异常信息
FileVisitResult postVisitDirectory(T dir, IOException ex) throws IOException;
}