import java.io.File;
import util.CopyFile;
public class CopyPackage {
public static void main(String[] args) {
copyPackage("F:\\JAVA\\day1","F:\\JAVA\\File");
}
public static void copyPackage(String srcPath,String destPath) {
//1.創建文件 確保目標地址存在 在目標文件夾下創建源文件的文件夾或文件
File srcFile = new File(srcPath);
File destFile = new File(destPath);
if (!destFile.exists()) {
destFile.mkdirs();
}
//如果源文件是文件,把文件拷貝到目標文件下
if (srcFile.isFile()) {
File f1 = new File(destFile.getAbsolutePath(),srcFile.getName());
CopyFile.copy(srcFile.getAbsolutePath(), f1.getAbsolutePath());
}else {//如果不存在,則在目標文件夾下創建一個文件夾
File f2 = new File(destFile.getAbsolutePath(),srcFile.getName());
f2.mkdirs();
File[] listFiles = srcFile.listFiles();
if (listFiles != null && listFiles.length>0) {
for(File list : listFiles) {
copyPackage(list.getAbsolutePath(), f2.getAbsolutePath());
}
}
}
}
}