第一种实现方式:
package com.io;
import java.io.*;
public class copy3 {
private static void copy(File file1, File file2) {//字节流文件拷贝
FileOutputStream fos = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
byte[] bytes = new byte[1024 * 1024];
int Count = 0;
while ((Count = fis.read(bytes)) != -1) {
fos.write(bytes, 0, Count);
}
fos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static void find(File file1, File file2) {
if (!file1.exists()) {//判断文件是否存在
System.out.println("没有这个文件!");
return;
}
File[] files;
files = file1.listFiles();//获取该目录下的所有子文件
if (files == null) {//判断是否有子文件
System.out.println("该目录下没有文件");
return;
}
for (File file : files) {//遍历目录
//endsWith 判断是否为"\\"结尾 使用三目运算符判断
File file3 = new File(file2.getAbsolutePath().endsWith("\\")?file2.getAbsolutePath():file2.getAbsolutePath() + "\\" + file.getName());//将file文件拷贝成为file3
if (file.isFile()) {//找到文件即进行拷贝
copy(file, file3);//拷贝方法
} else {//找到文件夹则继续深入查找
if (!file3.exists()) {
file3.mkdir();
}
find(file, file3);
}
}
}
public static void main(String[] args) {
File f1 = new File("D:\\code\\helloworld");//拷贝源
File f2 = new File("D:\\test");//拷贝目标
//将头部helloworld 这个大文件夹建立出来
File file3 = new File(f2.getAbsolutePath().endsWith("\\")?f2.getAbsolutePath():f2.getAbsolutePath() + "\\" + f1.getName());
if(!file3.exists()){
file3.mkdirs();
}
find(f1, file3);
}
}
第二种实现方式
package com.io;
import java.io.*;
public class CopyAll {
public static void main(String[] args) {
//拷贝源
File srcFile = new File("D:\\code\\helloworld");
//拷贝目标
File destFile = new File("D:\\资料");
//调用方法拷贝
copyDir(srcFile, destFile);
}
/*
拷贝方法:
拷贝源 srcFile
拷贝目标 destFile
*/
private static void copyDir(File srcFile, File destFile) {
if(srcFile.isFile()){
//srcFile是一个文件的话,递归结束
//是文件的话需要进行拷贝文件
//一遍读,一边写
FileInputStream in =null;
FileOutputStream out =null;
try {
in = new FileInputStream(srcFile);
String path=destFile.getAbsolutePath().endsWith("\\")?destFile.getAbsolutePath(): (destFile.getAbsolutePath()+"\\")+srcFile.getAbsolutePath().substring(3);;
out = new FileOutputStream(path);
byte[] bytes = new byte[1024*1024];
int readCount =0;
while((readCount=in.read(bytes))!=-1){
out.write(bytes,0,readCount);
}
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return ;
}
File[] files = srcFile.listFiles();
for (File file : files) {
//获取所有文件的绝对路径(包括目录和文件)
//System.out.println(file.getAbsolutePath());
if(file.isDirectory()){
//新建对应的目录
String srcDir = file.getAbsolutePath();
String destDir =destFile.getAbsolutePath().endsWith("\\")?destFile.getAbsolutePath(): (destFile.getAbsolutePath()+"\\")+srcDir.substring(3);
File newfile = new File(destDir);
if(!newfile.exists()){
newfile.mkdirs();
}
}
//递归调用
copyDir(file, destFile);
}
}
}