本文实例为大家分享了java实现系统多级文件夹复制的具体代码,供大家参考,具体内容如下
package com.jae;
import java.io.*;
//复制文件夹内的内容,包含多级文件夹
public class Test2 {
public static void main(String[] args) throws Exception {
//原文件夹地址
File resPath = new File("E:\\Java\\分享");
File destPath = new File("E:\\");
//method(resPath, destPath);
copy(resPath, destPath);
}
public static void copy(File src,File dest) throws Exception {
File newDir = new File(dest,src.getName());
newDir.mkdir();
File[] subFiles = src.listFiles();
for (File subFile : subFiles) {
if(subFile.isFile()){
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(subFile));
BufferedOutputStream bos =
new BufferedOutputStream(
new FileOutputStream(
new File(newDir,subFile.getName())));
int b;
byte[] bytes = new byte[1024 * 8];
while((b = bis.read(bytes)) != -1){
bos.write(bytes,0,b);
}
bis.close();
bos.close();
}else{
copy(subFile,newDir);//递归调用
}
}
}
public static void method(File dir, File destPath) throws IOException {
//获取源路径的文件
File[] files = dir.listFiles();
//根目录文件夹名
String name1 = dir.getName();
//遍历源路径的文件
for (File file : files) {
if (file.isFile()) {
StringBuilder sb = new StringBuilder(destPath.getAbsolutePath());
sb.append("\\").append(file.getName());
//获取盘符后面的路径和文件名
//String s = absolutePath.split(":")[1];
//创建输入流,封装获取到的文件的绝对路径
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
//目标路径,定义目标路径的盘符,和要复制的文件路径和文件名
//FileOutputStream fos = new FileOutputStream("F:" + s);
FileOutputStream fos = new FileOutputStream(sb.toString());
//复制文件操作
int len;
byte[] bytes = new byte[1024 * 8];
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
fos.flush();
}
fos.close();
fis.close();
} else {
StringBuilder sb = new StringBuilder(destPath.getAbsolutePath());
sb.append("\\").append(name1).append("\\").append(file.getName());
//创建文件夹路径
//File file1 = new File("F:" + name1);
File file1 = new File(sb.toString());
file1.mkdirs();
//System.out.println(name);
method(file, file1);
}
}
}
}
再为大家补充一段代码:Java递归复制多级目录及文件,感谢原作者的分享
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class C1 {
public static void main(String[] args) throws IOException {
File srcFolder = new File("D:\\1");
File dstFolder = new File("F:\\新建文件夹");
judge(srcFolder,dstFolder);
}
private static void judge(File srcFolder,File dstFolder) throws IOException {
if(srcFolder.isDirectory()){
File newFolder = new File(dstFolder,srcFolder.getName());
newFolder.mkdir();
File[] fileArr = srcFolder.listFiles();
for(File f:fileArr){
judge(f, newFolder);
}
}else{
File newFile = new File(dstFolder,srcFolder.getName());
// System.out.println(newFile);
copyFile(srcFolder,newFile);
}
}
private static void copyFile(File srcFolder, File newFile) throws IOException {
// TODO Auto-generated method stub
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcFolder));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(newFile));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。