/**
* 复制整个文件夹内容
*
* @param srcPath String 原文件路径
* @param destPath String 复制后路径
* @return boolean
*/
public static void dirCopy(String srcPath, String destPath) {
File src = new File(srcPath);
if (!new File(destPath).exists()) {
new File(destPath).mkdirs();
}
for (File s : src.listFiles()) {
if (s.isFile()) {
fileCopy(s.getPath(), destPath + File.separator + s.getName());
} else {
dirCopy(s.getPath(), destPath + File.separator + s.getName());
}
}
}
public static void fileCopy(String srcPath, String destPath) {
File src = new File(srcPath);
File dest = new File(destPath);
//使用jdk1.7 try-with-resource 释放资源,并添加了缓存流
try(InputStream is = new BufferedInputStream(new FileInputStream(src));
OutputStream out =new BufferedOutputStream(new FileOutputStream(dest))) {
byte[] flush = new byte[1024];
int len = -1;
while ((len = is.read(flush)) != -1) {
out.write(flush, 0, len);
}
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
原文:https://www.cnblogs.com/yyzhangneu/p/14467212.html