22
22
import java .nio .file .Files ;
23
23
import java .nio .file .Path ;
24
24
import java .nio .file .SimpleFileVisitor ;
25
+ import java .nio .file .StandardCopyOption ;
25
26
import java .nio .file .attribute .BasicFileAttributes ;
26
27
27
28
import org .springframework .lang .Nullable ;
@@ -51,15 +52,16 @@ public abstract class FileSystemUtils {
51
52
* otherwise {@code false}
52
53
*/
53
54
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 ;
61
64
}
62
- return false ;
63
65
}
64
66
65
67
/**
@@ -72,22 +74,26 @@ public static boolean deleteRecursively(@Nullable File root) {
72
74
* @since 5.0
73
75
*/
74
76
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 ;
89
79
}
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 ;
91
97
}
92
98
93
99
/**
@@ -125,7 +131,7 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th
125
131
}
126
132
@ Override
127
133
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 );
129
135
return FileVisitResult .CONTINUE ;
130
136
}
131
137
});
0 commit comments