java文件拷贝
2021-05-03 22:30
标签:abs out sof input ring base sql sts pre java文件拷贝 标签:abs out sof input ring base sql sts pre 原文地址:https://www.cnblogs.com/fmgao-technology/p/13196376.html文件拷贝
package com.sly.uploadfile.base;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* 文件拷贝
*/
public class CopyDir {
public static void main(String[] args) {
try {
copyDir("D:\\soft\\mysql", "D:\\tmp");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 拷贝目录
*/
public static void copyDir(String srcStr, String destStr) throws Exception {
File src = new File(srcStr);
File tempFile = new File(destStr + "//" + src.getName());
if (src.exists()) {
// 目录
if (src.isDirectory()) {
if (!tempFile.exists()) {
tempFile.mkdir();
}
File[] files = src.listFiles();
for (File f : files) {
copyDir(f.getAbsolutePath(), tempFile.getAbsolutePath());
}
} else {
// 文件
// 源文件
FileInputStream fin = new FileInputStream(srcStr);
// 目标文件
FileOutputStream fout = new FileOutputStream(destStr + "//" + src.getName());
int len = -1;
byte[] buffer = new byte[1024];
while ((len = fin.read(buffer)) != -1) {
fout.write(buffer, 0, len);
}
fout.close();
fin.close();
}
}
}
}