时间文件夹复制文件复制文件拷贝

/**
* 复制整个文件夹内容
*
* @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();
}
}
posted @ 2021-03-02 09:16  bnewky0319  阅读(79)  评论(0编辑  收藏  举报