工具方法
public static void copy(File source, File target, boolean isOverride) throws IOException {
final StandardCopyOption option = isOverride ? StandardCopyOption.REPLACE_EXISTING : StandardCopyOption.COPY_ATTRIBUTES;
if (source.isFile())
Files.copy(source.toPath(), target.toPath(), option);
else {
if (!target.exists())
target.mkdirs();
File[] sourceFiles = source.listFiles();
for (File file : sourceFiles) {
if (file.isFile())
Files.copy(file.toPath(), Paths.get(target.getPath(), file.getName()), option);
else
copy(file, new File(target.getPath() + File.separator + file.getName()), isOverride);
}
}
}
public static void copy(String source, String target, boolean isOverride) throws IOException {
copy(new File(source), new File(target), isOverride);
}
引用的类
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
相关方法
示例
public static void main(String[] args) {
String source = "D:\\imgPath";
String target = "D:\\imgPath2";
try {
copy(source, target, true);
} catch (IOException e) {
e.printStackTrace();
}
}