递归复制

package cn.itcast.Io;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class FileCopyDemo {


public static void main(String[] args) throws Exception {
File file=new File("F:\\a");
File file2=new File("E:\\c");
FileCopy(file,file2);


}


/**
* 1.如果是文件夹先追加文件夹
* 2.如果是文件直接复制
* 3.递归操作上述步骤。
*   文件夹的源头和目的地是每次都是不断改变的
*/


public static void  FileCopy(File  src ,File desc) throws Exception{
if(src.isDirectory()){
String name = src.getName();
File  temp=new File(desc,name);
temp.mkdirs();
       //1到此步得到了与之相仿的文件夹  最终的文件目的地。如果此时的文件夹下面还有文件夹的话 继续拼接文件

//走递归流程
File[] files = src.listFiles();
for (File file2 : files) {
FileCopy(file2,temp);
}
 
}else{
//2复制
String name = src.getName();
Copy(src,new File(desc,name));
}
}



public static  void  Copy(File  src,File desc) throws Exception{
BufferedOutputStream  output=new   BufferedOutputStream(new FileOutputStream(desc));
BufferedInputStream   input =new BufferedInputStream(new FileInputStream(src));
byte[]  b=new byte[1024];
int len;
while((len=input.read(b))!=-1){
output.write(b,0,len);
}
output.close();
input.close();
}
}



































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值