-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchFiles.java
More file actions
49 lines (40 loc) · 1.32 KB
/
SearchFiles.java
File metadata and controls
49 lines (40 loc) · 1.32 KB
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
package ru.job4j.searchtool;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import static java.nio.file.FileVisitResult.CONTINUE;
public class SearchFiles implements FileVisitor<Path> {
private final Predicate<Path> condition;
private final List<Path> pathList = new ArrayList<>();
public SearchFiles(Predicate<Path> condition) {
this.condition = condition;
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
throws IOException {
return CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (condition.test(file.getFileName())) {
pathList.add(file);
}
return CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return CONTINUE;
}
public List<Path> getPaths() {
return pathList;
}
}