2 * Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
3 * This source code is licensed under both the GPLv2 (found in the
4 * COPYING file in the root directory) and Apache 2.0 License
5 * (found in the LICENSE.Apache file in the root directory).
7 package org
.rocksdb
.util
;
9 import java
.io
.IOException
;
10 import java
.nio
.file
.FileVisitResult
;
11 import java
.nio
.file
.Files
;
12 import java
.nio
.file
.Path
;
13 import java
.nio
.file
.SimpleFileVisitor
;
14 import java
.nio
.file
.attribute
.BasicFileAttributes
;
16 public final class FileUtils
{
17 private static final SimpleFileVisitor
<Path
> DELETE_DIR_VISITOR
= new DeleteDirVisitor();
20 * Deletes a path from the filesystem
22 * If the path is a directory its contents
23 * will be recursively deleted before it itself
26 * Note that removal of a directory is not an atomic-operation
27 * and so if an error occurs during removal, some of the directories
28 * descendants may have already been removed
30 * @param path the path to delete.
32 * @throws IOException if an error occurs whilst removing a file or directory
34 public static void delete(final Path path
) throws IOException
{
35 if (!Files
.isDirectory(path
)) {
36 Files
.deleteIfExists(path
);
38 Files
.walkFileTree(path
, DELETE_DIR_VISITOR
);
42 private static class DeleteDirVisitor
extends SimpleFileVisitor
<Path
> {
44 public FileVisitResult
visitFile(final Path file
, final BasicFileAttributes attrs
) throws IOException
{
45 Files
.deleteIfExists(file
);
46 return FileVisitResult
.CONTINUE
;
50 public FileVisitResult
postVisitDirectory(final Path dir
, final IOException exc
) throws IOException
{
55 Files
.deleteIfExists(dir
);
56 return FileVisitResult
.CONTINUE
;