-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathWalkFileTreePattern.java
More file actions
53 lines (36 loc) · 1.54 KB
/
WalkFileTreePattern.java
File metadata and controls
53 lines (36 loc) · 1.54 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
50
51
52
53
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.io.IOException;
public class WalkFileTreePattern {
public static void main(String[] args) throws IOException {
Path path = Paths.get("files/");
boolean directory = Files.isDirectory(path);
System.out.println("Directory = " + directory);
var visitor = new FileVisitor<Path>(){
private long countFiles = 0L;
private long countDirs = 0L;
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
countDirs++;
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
countFiles++;
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return null;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
};
Files.walkFileTree(path, visitor);
System.out.println("Number of files = " + visitor.countFiles);
System.out.println("Number of directories = " + visitor.countDirs);
}
}