2222import java .nio .file .Files ;
2323import java .nio .file .Path ;
2424import java .nio .file .SimpleFileVisitor ;
25+ import java .nio .file .StandardCopyOption ;
2526import java .nio .file .attribute .BasicFileAttributes ;
2627
2728import org .springframework .lang .Nullable ;
@@ -51,15 +52,16 @@ public abstract class FileSystemUtils {
5152 * otherwise {@code false}
5253 */
5354 public static boolean deleteRecursively (@ Nullable File root ) {
54- if (root != null ) {
55- try {
56- return deleteRecursively (root .toPath ());
57- }
58- catch (IOException ex ) {
59- return false ;
60- }
55+ if (root == null ) {
56+ return false ;
57+ }
58+
59+ try {
60+ return deleteRecursively (root .toPath ());
61+ }
62+ catch (IOException ex ) {
63+ return false ;
6164 }
62- return false ;
6365 }
6466
6567 /**
@@ -72,22 +74,26 @@ public static boolean deleteRecursively(@Nullable File root) {
7274 * @since 5.0
7375 */
7476 public static boolean deleteRecursively (@ Nullable Path root ) throws IOException {
75- if (root != null ) {
76- Files .walkFileTree (root , new SimpleFileVisitor <Path >() {
77- @ Override
78- public FileVisitResult visitFile (Path file , BasicFileAttributes attrs ) throws IOException {
79- Files .delete (file );
80- return FileVisitResult .CONTINUE ;
81- }
82- @ Override
83- public FileVisitResult postVisitDirectory (Path dir , IOException exc ) throws IOException {
84- Files .delete (dir );
85- return FileVisitResult .CONTINUE ;
86- }
87- });
88- return Files .deleteIfExists (root );
77+ if (root == null ) {
78+ return false ;
8979 }
90- return false ;
80+ if (!Files .exists (root )) {
81+ return false ;
82+ }
83+
84+ Files .walkFileTree (root , new SimpleFileVisitor <Path >() {
85+ @ Override
86+ public FileVisitResult visitFile (Path file , BasicFileAttributes attrs ) throws IOException {
87+ Files .delete (file );
88+ return FileVisitResult .CONTINUE ;
89+ }
90+ @ Override
91+ public FileVisitResult postVisitDirectory (Path dir , IOException exc ) throws IOException {
92+ Files .delete (dir );
93+ return FileVisitResult .CONTINUE ;
94+ }
95+ });
96+ return true ;
9197 }
9298
9399 /**
@@ -125,7 +131,7 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th
125131 }
126132 @ Override
127133 public FileVisitResult visitFile (Path file , BasicFileAttributes attrs ) throws IOException {
128- Files .copy (file , dest .resolve (src .relativize (file )));
134+ Files .copy (file , dest .resolve (src .relativize (file )), StandardCopyOption . REPLACE_EXISTING );
129135 return FileVisitResult .CONTINUE ;
130136 }
131137 });
0 commit comments