-
Notifications
You must be signed in to change notification settings - Fork 2
/
FilesUtil.groovy
54 lines (41 loc) · 1.36 KB
/
FilesUtil.groovy
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
package carnival.util
import java.nio.file.Path
import java.nio.file.Files
import java.nio.file.SimpleFileVisitor
import java.nio.file.FileVisitResult
import java.nio.file.attribute.BasicFileAttributes
/**
* Static utility methods for dealing with files.
*/
class FilesUtil {
/**
* Delete all the files starting at the provided start path; if the path is
* a file, delete the file; if the path is a directory, delete everything
* in the directory and the directory itself.
* @param start The path to delete
* @return The starting path
*/
static Path delete(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;
}
}
})
}
}