public void copy(String srcPath, String destPath) throws IOException{
File srcFile = new File(srcPath);
File destFile = new File(destPath);
if (srcFile.exists()) {
if (!destFile.exists()) {
destFile.mkdir();
}
for (File file : srcFile.listFiles()) {
if (file.isFile()) { // 如果是文件就直接复制
String path = destPath + file.getName();
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(new File(path));
byte[] buf = new byte[2048];
int index = -1;
while ((index = fis.read(buf)) != -1) {
fos.write(buf, 0, index);
}
fis.close();
fos.flush();
fos.close();
} else if (file.isDirectory()) { // 如果是文件夹则递归进入子文件。
String path = file.getName() + "/";
copy(srcPath + path, destPath + path);
}
}
}
}
Java 实现文件复制方法
最新推荐文章于 2021-03-13 10:21:47 发布