[io] Adjust copyRecursive behavior for files
This commit is contained in:
parent
29bda2154c
commit
50442e31e3
|
@ -95,26 +95,27 @@ public class JFiles {
|
|||
}
|
||||
|
||||
/**
|
||||
* If the source is a file, copy it to the target. If it is a directory, create the target directory if it doesn't exist and copy the source directories content there
|
||||
* If the source is a file, copy it to the destination. If it is a directory, create the destination directory if it doesn't exist and copy the source directories' content there
|
||||
* @param source The path to copy from
|
||||
* @param target The path to copy to
|
||||
* @param destination The path to copy to
|
||||
* @param copyOptions Copy options to use
|
||||
* @throws IOException Something went wrong
|
||||
*/
|
||||
public static void copyRecursive(Path source, Path target, CopyOption... copyOptions) throws IOException {
|
||||
if (!Files.exists(target)) Files.createDirectories(target);
|
||||
public static void copyRecursive(Path source, Path destination, CopyOption... copyOptions) throws IOException {
|
||||
boolean replaceExisting = Arrays.asList(copyOptions).contains(StandardCopyOption.REPLACE_EXISTING);
|
||||
if (Files.isDirectory(source)) {
|
||||
Files.createDirectories(destination);
|
||||
listTo(source, sourceResolved -> {
|
||||
Path targetResolved = target.resolve(sourceResolved.getFileName().toString());
|
||||
if (Files.exists(target)) {
|
||||
Path targetResolved = destination.resolve(sourceResolved.getFileName().toString());
|
||||
if (Files.exists(destination)) {
|
||||
if (!replaceExisting) return;
|
||||
if (!Files.isDirectory(sourceResolved)) Files.delete(target);
|
||||
if (!Files.isDirectory(sourceResolved)) Files.delete(destination);
|
||||
}
|
||||
copyRecursive(sourceResolved, targetResolved, copyOptions);
|
||||
});
|
||||
} else if (Files.exists(source)) {
|
||||
if (!Files.exists(target) || replaceExisting) Files.copy(source, target, copyOptions);
|
||||
if (destination.getParent() != null) Files.createDirectories(destination.getParent());
|
||||
if (!Files.exists(destination) || replaceExisting) Files.copy(source, destination, copyOptions);
|
||||
} else throw new FileNotFoundException(source.toString());
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue