1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.datamodel.niofs.utils;
20
21 import java.io.IOException;
22 import java.nio.file.FileVisitResult;
23 import java.nio.file.FileVisitor;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.nio.file.SimpleFileVisitor;
27 import java.nio.file.attribute.BasicFileAttributes;
28
29
30
31
32
33
34
35
36
37 public final class MCRRecursiveDeleter extends SimpleFileVisitor<Path> {
38
39 private static final MCRRecursiveDeleter INSTANCE = new MCRRecursiveDeleter();
40
41 private MCRRecursiveDeleter() {
42 }
43
44 public static MCRRecursiveDeleter instance() {
45 return INSTANCE;
46 }
47
48 @Override
49 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
50 Files.delete(file);
51 return super.visitFile(file, attrs);
52 }
53
54 @Override
55 public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
56 if (exc != null) {
57 throw exc;
58 }
59 if (dir.getNameCount() > 0) {
60 Files.delete(dir);
61 }
62 return super.postVisitDirectory(dir, exc);
63 }
64 }