]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.util/src/org/argeo/util/FsUtils.java
Fix anonymous login
[lgpl/argeo-commons.git] / org.argeo.util / src / org / argeo / util / FsUtils.java
1 package org.argeo.util;
2
3 import java.io.IOException;
4 import java.nio.file.FileVisitResult;
5 import java.nio.file.Files;
6 import java.nio.file.Path;
7 import java.nio.file.SimpleFileVisitor;
8 import java.nio.file.attribute.BasicFileAttributes;
9
10 /** Utilities around the standard Java file abstractions. */
11 public class FsUtils {
12 /**
13 * Deletes this path, recursively if needed. Does nothing if the path does not
14 * exist.
15 */
16 public static void delete(Path path) {
17 try {
18 if (!Files.exists(path))
19 return;
20 Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
21 @Override
22 public FileVisitResult postVisitDirectory(Path directory, IOException e) throws IOException {
23 if (e != null)
24 throw e;
25 Files.delete(directory);
26 return FileVisitResult.CONTINUE;
27 }
28
29 @Override
30 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
31 Files.delete(file);
32 return FileVisitResult.CONTINUE;
33 }
34 });
35 } catch (IOException e) {
36 throw new RuntimeException("Cannot delete " + path, e);
37 }
38 }
39
40 /** Singleton. */
41 private FsUtils() {
42 }
43
44 }