Java把一文件夹的所有内容完全复制于另一文件夹中

package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class Application{
     public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        String srcFolder=cin.nextLine();
        String destFolder=cin.nextLine();
        copyFolder(srcFolder, destFolder);
     }
     public static void copyFolder(String srcFolder,String destFolder) {
         File file=new File(srcFolder);
         File file2=new File(destFolder);
         byte[] b=new byte[(int)file.length()];
         dfs(file,destFolder);
     }
     public static void copyFile(File srcfile,String destFolder) {
         String string=destFolder+"\\"+srcfile.getName();
         File file=new File(string);
         file.getParentFile().mkdirs();
         try {
            file.createNewFile();
        } catch (IOException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
         byte[] b=null;
        try(FileInputStream fis=new FileInputStream(srcfile)) {
            b=new byte[(int)srcfile.length()];
            fis.read(b);
            
        } catch (IOException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        try (FileOutputStream fos=new FileOutputStream(file)){
            fos.write(b);
        } catch (IOException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
     }
     public static void dfs(File file,String destFolder) {
         String strtemp=destFolder;
         File[] fs=file.listFiles();
         for(File f:fs)
         {
             if(f.isDirectory()) {     //文件夹继续深搜
                String str=destFolder+"\\"+f.getName();
                File file2=new File(str);
                file2.mkdirs();
                destFolder=destFolder+"\\"+file2.getName();
                dfs(f,destFolder);
                destFolder=strtemp;
             }
             else {
                copyFile(f,destFolder);
            }

         }
     }
}

更多Java详细内容:

http://how2j.cn/p/3046

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值