forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteFileVisitor.java
More file actions
55 lines (41 loc) · 1.52 KB
/
DeleteFileVisitor.java
File metadata and controls
55 lines (41 loc) · 1.52 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
54
55
package modern.challenge;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
public class DeleteFileVisitor implements FileVisitor {
@Override
public FileVisitResult postVisitDirectory(Object dir, IOException ioe) throws IOException {
System.out.println("Visited: " + (Path) dir);
boolean deleted = delete((Path) dir);
if (deleted) {
System.out.println("Deleted: " + (Path) dir);
} else {
System.out.println("Not deleted: " + (Path) dir);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Object file, BasicFileAttributes attrs) throws IOException {
boolean deleted = delete((Path) file);
if (deleted) {
System.out.println("Deleted: " + (Path) file);
} else {
System.out.println("Not deleted: " + (Path) file);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Object file, IOException ioe) throws IOException {
return FileVisitResult.CONTINUE;
}
private static boolean delete(Path file) throws IOException {
return Files.deleteIfExists(file);
}
}