Skip to content

Commit fabc9c2

Browse files
committed
Align new FileSystemUtils NIO implementation with original behavior
Issue: SPR-15845 Issue: SPR-15846
1 parent 08dfce2 commit fabc9c2

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

spring-core/src/main/java/org/springframework/util/FileSystemUtils.java

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.nio.file.Files;
2323
import java.nio.file.Path;
2424
import java.nio.file.SimpleFileVisitor;
25+
import java.nio.file.StandardCopyOption;
2526
import java.nio.file.attribute.BasicFileAttributes;
2627

2728
import 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

Comments
 (0)