复制文件夹中的所有文件和文件夹到另一个文件夹中,因为这个需求相对比较简单,这里就直接上代码了。
【示例】复制文件夹操作
/**
* 复制文件夹操作
* @param sourceDir 源文件夹
* @param destDir 目标文件夹
* @throws FileNotFoundException
*/
public static void copyDir(File sourceFile, File destFile) throws FileNotFoundException {
// 1.判断源文件是否存在
if(!sourceFile.exists()) {
throw new FileNotFoundException("源文件夹不存在!");
}
// 2.如果目标文件不存在,则新建目标文件夹
if(!destFile.exists()) {
destFile.mkdirs();
}
// 3.获取源文件夹中所有的子文件(包含文件和文件夹)
File[] files = sourceFile.listFiles();
for(File file : files) {