Align new FileSystemUtils NIO implementation with original behavior

Issue: SPR-15845
Issue: SPR-15846
This commit is contained in:
Juergen Hoeller 2017-08-03 14:21:00 +02:00
parent 08dfce2cb5
commit fabc9c28d7
1 changed files with 30 additions and 24 deletions

View File

@ -22,6 +22,7 @@ import java.nio.file.FileVisitResult;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor; import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
@ -51,15 +52,16 @@ public abstract class FileSystemUtils {
* otherwise {@code false} * otherwise {@code false}
*/ */
public static boolean deleteRecursively(@Nullable File root) { public static boolean deleteRecursively(@Nullable File root) {
if (root != null) { if (root == null) {
try { return false;
return deleteRecursively(root.toPath()); }
}
catch (IOException ex) { try {
return false; return deleteRecursively(root.toPath());
} }
catch (IOException ex) {
return false;
} }
return false;
} }
/** /**
@ -72,22 +74,26 @@ public abstract class FileSystemUtils {
* @since 5.0 * @since 5.0
*/ */
public static boolean deleteRecursively(@Nullable Path root) throws IOException { public static boolean deleteRecursively(@Nullable Path root) throws IOException {
if (root != null) { if (root == null) {
Files.walkFileTree(root, new SimpleFileVisitor<Path>() { return false;
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
return Files.deleteIfExists(root);
} }
return false; if (!Files.exists(root)) {
return false;
}
Files.walkFileTree(root, 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 exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
return true;
} }
/** /**
@ -125,7 +131,7 @@ public abstract class FileSystemUtils {
} }
@Override @Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.copy(file, dest.resolve(src.relativize(file))); Files.copy(file, dest.resolve(src.relativize(file)), StandardCopyOption.REPLACE_EXISTING);
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} }
}); });