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
;
10 /** Utilities around the standard Java file abstractions. */
11 public class FsUtils
{
12 /** Sync a source path with a target path. */
13 public static void sync(Path sourceBasePath
, Path targetBasePath
) {
14 sync(sourceBasePath
, targetBasePath
, false);
17 /** Sync a source path with a target path. */
18 public static void sync(Path sourceBasePath
, Path targetBasePath
, boolean delete
) {
19 sync(new BasicSyncFileVisitor(sourceBasePath
, targetBasePath
, delete
, true));
22 public static void sync(BasicSyncFileVisitor syncFileVisitor
) {
24 Files
.walkFileTree(syncFileVisitor
.getSourceBasePath(), syncFileVisitor
);
25 } catch (Exception e
) {
26 throw new RuntimeException("Cannot sync " + syncFileVisitor
.getSourceBasePath() + " with "
27 + syncFileVisitor
.getTargetBasePath(), e
);
31 /** Deletes this path, recursively if needed. */
32 public static void delete(Path path
) {
34 Files
.walkFileTree(path
, new SimpleFileVisitor
<Path
>() {
36 public FileVisitResult
postVisitDirectory(Path directory
, IOException e
) throws IOException
{
39 Files
.delete(directory
);
40 return FileVisitResult
.CONTINUE
;
44 public FileVisitResult
visitFile(Path file
, BasicFileAttributes attrs
) throws IOException
{
46 return FileVisitResult
.CONTINUE
;
49 } catch (IOException e
) {
50 throw new RuntimeException("Cannot delete " + path
, e
);