Java基础知识之将源目标中的所有文件拷贝到另一个文件当中

import java.io.*;

/**
 * 将F:\学校文档\编程语言中的所有文件复制到D:\a\b\c\test
 */
public class CopyAll {
    public static void main(String[] args) {
        // 拷贝源
        File srcFile=new File("F:\\学校文档\\编程语言");
        // 拷贝目标
        File destFile=new File("D:\\a\\b\\c\\test");
        System.out.println(destFile.getAbsolutePath());//D:\a\b\c\test
        // 调用方法拷贝
       copyDir(srcFile,destFile);
    }

    /**
     * 拷贝目录
     * @param srcFile 拷贝源
     * @param destFile  拷贝目标
     */
    private static void copyDir(File srcFile, File destFile) {
        // srcFile如果是一个文件的话,递归结束。
        // 是文件的时候需要拷贝。
        // ....一边读一边写。
        if(srcFile.isFile()){
        FileInputStream fis=null;
        FileOutputStream fos=null;

        try {
            // 读源文件 F:\学校文档\编程语言
            fis=new FileInputStream(srcFile);
            //判断目标文件D:\a\b\c\test的绝对路径是否以\结尾
            //如果是则直接将源文件的绝对路径F:\去掉加到目标路径D:\a\b\c\test之后
            //变成新的绝对路径D:\a\b\c\test\学校文档\编程语言
            //如果不是以\结尾则加上\在执行以上操作
            String path=(destFile.getAbsolutePath().endsWith("\\")?destFile.getAbsolutePath():destFile.getAbsolutePath()+"\\")
                    +srcFile.getAbsolutePath().substring(3);
            // 将新的目标文件绝对路径path写入
            fos=new FileOutputStream(path);
            // 一边读一边写 一次读1MB
            byte[] bytes=new byte[1024*1024];
            int readCount=0;
            while((readCount=fis.read(bytes))!=-1){
                fos.write(bytes,0,readCount);
            }

            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } 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();
                }
            }
        }
        return;
        }


        //代码执行到此处说明srcFile是一个目录则获取源目录下面的子目录
        File[] files=srcFile.listFiles();
        for (File f:files) {
            if(f.isDirectory()){
                //获取子目录的绝对路径
                String srcDir=f.getAbsolutePath();
                String desDir=(destFile.getAbsolutePath().endsWith("\\")?destFile.getAbsolutePath():destFile.getAbsolutePath()+"\\")
                        +srcDir.substring(3);

                //新建对应目录
                File newFile=new File(desDir);
                if(!newFile.exists()){
                    newFile.mkdirs();
                }
            }
            //递归调用
            copyDir(f,destFile);
        }
    }
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值