MENU

IO流复制文件——做一个文件拷贝的小练习(IO流)

• November 21, 2020 • java高级特性,小项目

以前学过IO流,那时候也做了很多联系,不过好像忘了一些了,现在做个小练习巩固一下,主要针对子文件夹拷贝的递归操作
现在我的D盘有两个文件夹dir1(内含子文件夹和文件)和dir2(空),我想把整个dir1拷贝到dir2下面

视频演示

dir1和dir2

dir1中的文件和子文件夹

dir2现在为空,待会我们要把dir1中的所有拷贝到这里来

代码实现

import java.io.*;

class FileUtil {
    private File srcFile;
    private File desFile;
    /**
     * 构造
     */
    public FileUtil(String oldFile, String newFile) {
        // 利用路径字符串来创建文件
        this.srcFile = new File(oldFile);
        // 新文件 = 新文件所在的路径 + 系统分隔符 + 原文件的路径
        this.desFile = new File(newFile + File.separator + this.srcFile.getName());
    }
    public FileUtil(File srcFile, File desFile) {
        this.srcFile = srcFile;
        this.desFile = desFile;
    }
    /**
     * 拷贝
     */
    public boolean copy() {
        if (!srcFile.exists()) {
            System.out.println("原文件不存在!");
            return false;
        }
        try {
            copyImpy(srcFile);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    /**
     * 复制的核心代码,利用了递归
     */
    public void copyImpy(File file) throws IOException {
        // 判断是一个目录
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File f : files) {
                    copyImpy(f);
                }
            }
        }
        // 判断是一个文件
        else {
            // 文件目录 = 原文件路径 - 原文件最外层路径 - 系统分隔符
            String path = file.getPath().replace(srcFile.getPath() + File.separator, "");
            // 新文件路径(包含子文件夹) = 目的文件夹 + 子文件夹路径
            File newFile = new File(this.desFile, path);
            // 调用文件拷贝函数
            copyFileImpl(file, newFile);
        }
    }
    /**
     * 复制文件
     */
    public void copyFileImpl(File srcFile, File desFile) throws IOException {
        // 如果目标路径不存在,就创建路径,可能有多层文件夹,故用mkdirs()
        if (!desFile.getParentFile().exists()) {
            desFile.getParentFile().mkdirs();
        }
        InputStream inputStream = new FileInputStream(srcFile);
        OutputStream outputStream = new FileOutputStream(desFile);
        // 后台输出拷贝情况
        System.out.println("正在从 " + srcFile.getParent() + " 拷贝文件 " + srcFile.getName() + " 到" + desFile.getParent());
        // 字节数组,相当于一个缓冲区
        byte[] date = new byte[1024];
        // 一次性读出的字节长度
        int len = 0;
        while ((len = inputStream.read(date)) != -1) {
            // 将缓存数组的数据写入到新文件
            outputStream.write(date, 0, len);
        }
        // 关闭流
        inputStream.close();
        outputStream.close();
    }
}

public class Main {
    public static void main(String[] args) {
        // 两个参数,一个表示原文件及路径,一个表示目的路径
        if (args.length != 2) {
            System.out.println("初始化数据错误!");
            return;
        }
        FileUtil fileUtil = new FileUtil(args[0], args[1]);
        if (fileUtil.copy()) {
            System.out.println("拷贝成功!");
        } else {
            System.out.println("拷贝失败!");
        }
    }
}

控制台实时输出

正在从 D:dir1 拷贝文件 boot-crm.rar 到D:dir2dir1
正在从 D:dir1 拷贝文件 idea使用教程2017-06-01.pdf 到D:dir2dir1
正在从 D:dir1 拷贝文件 ssmchapter01.rar 到D:dir2dir1
正在从 D:dir1ssmchapter02ssmchapter02 拷贝文件 pom.xml 到D:dir2dir1ssmchapter02ssmchapter02
正在从 D:dir1ssmchapter02ssmchapter02srcmainJavacomssmannotation 拷贝文件 AnnotationAssembleTest.java 到D:dir2dir1ssmchapter02ssmchapter02srcmainJavacomssmannotation
正在从 D:dir1ssmchapter02ssmchapter02srcmainJavacomssmannotation 拷贝文件 UserController.java 到D:dir2dir1ssmchapter02ssmchapter02srcmainJavacomssmannotation
正在从 D:dir1ssmchapter02ssmchapter02srcmainJavacomssmannotation 拷贝文件 UserDao.java 到D:dir2dir1ssmchapter02ssmchapter02srcmainJavacomssmannotation
正在从 D:dir1ssmchapter02ssmchapter02srcmainJavacomssmannotation 拷贝文件 UserDaoImpl.java 到D:dir2dir1ssmchapter02ssmchapter02srcmainJavacomssmannotation
正在从 D:dir1ssmchapter02ssmchapter02srcmainJavacomssmannotation 拷贝文件 UserService.java 到D:dir2dir1ssmchapter02ssmchapter02srcmainJavacomssmannotation
正在从 D:dir1ssmchapter02ssmchapter02srcmainJavacomssmannotation 拷贝文件 UserServiceImpl.java 到D:dir2dir1ssmchapter02ssmchapter02srcmainJavacomssmannotation
正在从 D:dir1ssmchapter02ssmchapter02srcmainJavacomssmassemble 拷贝文件 User.java 到D:dir2dir1ssmchapter02ssmchapter02srcmainJavacomssmassemble
正在从 D:dir1ssmchapter02ssmchapter02srcmainJavacomssmassemble 拷贝文件 XmlBeanAssembleTest.java 到D:dir2dir1ssmchapter02ssmchapter02srcmainJavacomssmassemble
正在从 D:dir1ssmchapter02ssmchapter02srcmainJavacomssminstanceconstructor 拷贝文件 Bean1.java 到D:dir2dir1ssmchapter02ssmchapter02srcmainJavacomssminstanceconstructor
正在从 D:dir1ssmchapter02ssmchapter02srcmainJavacomssminstanceconstructor 拷贝文件 InstanceTest1.java 到D:dir2dir1ssmchapter02ssmchapter02srcmainJavacomssminstanceconstructor
正在从 D:dir1ssmchapter02ssmchapter02srcmainJavacomssminstancefactory 拷贝文件 Bean3.java 到D:dir2dir1ssmchapter02ssmchapter02srcmainJavacomssminstancefactory
正在从 D:dir1ssmchapter02ssmchapter02srcmainJavacomssminstancefactory 拷贝文件 InstanceTest3.java 到D:dir2dir1ssmchapter02ssmchapter02srcmainJavacomssminstancefactory
正在从 D:dir1ssmchapter02ssmchapter02srcmainJavacomssminstancefactory 拷贝文件 MyBean3Factory.java 到D:dir2dir1ssmchapter02ssmchapter02srcmainJavacomssminstancefactory
正在从 D:dir1ssmchapter02ssmchapter02srcmainJavacomssminstancestatic_factory 拷贝文件 Bean2.java 到D:dir2dir1ssmchapter02ssmchapter02srcmainJavacomssminstancestatic_factory
正在从 D:dir1ssmchapter02ssmchapter02srcmainJavacomssminstancestatic_factory 拷贝文件 InstanceTest2.java 到D:dir2dir1ssmchapter02ssmchapter02srcmainJavacomssminstancestatic_factory
正在从 D:dir1ssmchapter02ssmchapter02srcmainJavacomssminstancestatic_factory 拷贝文件 MyBean2Factory.java 到D:dir2dir1ssmchapter02ssmchapter02srcmainJavacomssminstancestatic_factory
正在从 D:dir1ssmchapter02ssmchapter02srcmainJavacomssmscope 拷贝文件 Scope.java 到D:dir2dir1ssmchapter02ssmchapter02srcmainJavacomssmscope
正在从 D:dir1ssmchapter02ssmchapter02srcmainJavacomssmscope 拷贝文件 ScopeTest.java 到D:dir2dir1ssmchapter02ssmchapter02srcmainJavacomssmscope
正在从 D:dir1ssmchapter02ssmchapter02srcmainresources 拷贝文件 beans1.xml 到D:dir2dir1ssmchapter02ssmchapter02srcmainresources
正在从 D:dir1ssmchapter02ssmchapter02srcmainresources 拷贝文件 beans2.xml 到D:dir2dir1ssmchapter02ssmchapter02srcmainresources
正在从 D:dir1ssmchapter02ssmchapter02srcmainresources 拷贝文件 beans3.xml 到D:dir2dir1ssmchapter02ssmchapter02srcmainresources
正在从 D:dir1ssmchapter02ssmchapter02srcmainresources 拷贝文件 beans4.xml 到D:dir2dir1ssmchapter02ssmchapter02srcmainresources
正在从 D:dir1ssmchapter02ssmchapter02srcmainresources 拷贝文件 beans5.xml 到D:dir2dir1ssmchapter02ssmchapter02srcmainresources
正在从 D:dir1ssmchapter02ssmchapter02srcmainresources 拷贝文件 beans6.xml 到D:dir2dir1ssmchapter02ssmchapter02srcmainresources
正在从 D:dir1ssmchapter02ssmchapter02srcmainwebapp 拷贝文件 index.jsp 到D:dir2dir1ssmchapter02ssmchapter02srcmainwebapp
正在从 D:dir1ssmchapter02ssmchapter02srcmainwebappWEB-INF 拷贝文件 web.xml 到D:dir2dir1ssmchapter02ssmchapter02srcmainwebappWEB-INF
正在从 D:dir1ssmchapter02 拷贝文件 第2章 Spring中的Bean.pdf 到D:dir2dir1ssmchapter02
正在从 D:dir1 拷贝文件 ssmchapter02.rar 到D:dir2dir1
正在从 D:dir1ssmchapter03ssmchapter03 拷贝文件 pom.xml 到D:dir2dir1ssmchapter03ssmchapter03
正在从 D:dir1ssmchapter03ssmchapter03srcmainJavacomssmaspect 拷贝文件 MyAspect.java 到D:dir2dir1ssmchapter03ssmchapter03srcmainJavacomssmaspect
正在从 D:dir1ssmchapter03ssmchapter03srcmainJavacomssmaspectjannotation 拷贝文件 MyAspect.java 到D:dir2dir1ssmchapter03ssmchapter03srcmainJavacomssmaspectjannotation
正在从 D:dir1ssmchapter03ssmchapter03srcmainJavacomssmaspectjannotation 拷贝文件 TestAnnotationAspectj.java 到D:dir2dir1ssmchapter03ssmchapter03srcmainJavacomssmaspectjannotation
正在从 D:dir1ssmchapter03ssmchapter03srcmainJavacomssmaspectjxml 拷贝文件 MyAspect.java 到D:dir2dir1ssmchapter03ssmchapter03srcmainJavacomssmaspectjxml
正在从 D:dir1ssmchapter03ssmchapter03srcmainJavacomssmaspectjxml 拷贝文件 TestXmlAspectj.java 到D:dir2dir1ssmchapter03ssmchapter03srcmainJavacomssmaspectjxml
正在从 D:dir1ssmchapter03ssmchapter03srcmainJavacomssmcglib 拷贝文件 CglibProxy.java 到D:dir2dir1ssmchapter03ssmchapter03srcmainJavacomssmcglib
正在从 D:dir1ssmchapter03ssmchapter03srcmainJavacomssmcglib 拷贝文件 CglibTest.java 到D:dir2dir1ssmchapter03ssmchapter03srcmainJavacomssmcglib
正在从 D:dir1ssmchapter03ssmchapter03srcmainJavacomssmcglib 拷贝文件 UserDao.java 到D:dir2dir1ssmchapter03ssmchapter03srcmainJavacomssmcglib
正在从 D:dir1ssmchapter03ssmchapter03srcmainJavacomssmfactorybean 拷贝文件 MyAspect.java 到D:dir2dir1ssmchapter03ssmchapter03srcmainJavacomssmfactorybean
正在从 D:dir1ssmchapter03ssmchapter03srcmainJavacomssmfactorybean 拷贝文件 ProxyFactoryBeanTest.java 到D:dir2dir1ssmchapter03ssmchapter03srcmainJavacomssmfactorybean
正在从 D:dir1ssmchapter03ssmchapter03srcmainJavacomssmjdk 拷贝文件 JdkProxy.java 到D:dir2dir1ssmchapter03ssmchapter03srcmainJavacomssmjdk
正在从 D:dir1ssmchapter03ssmchapter03srcmainJavacomssmjdk 拷贝文件 JdkTest.java 到D:dir2dir1ssmchapter03ssmchapter03srcmainJavacomssmjdk
正在从 D:dir1ssmchapter03ssmchapter03srcmainJavacomssmjdk 拷贝文件 UserDao.java 到D:dir2dir1ssmchapter03ssmchapter03srcmainJavacomssmjdk
正在从 D:dir1ssmchapter03ssmchapter03srcmainJavacomssmjdk 拷贝文件 UserDaoImpl.java 到D:dir2dir1ssmchapter03ssmchapter03srcmainJavacomssmjdk
正在从 D:dir1ssmchapter03ssmchapter03srcmainresources 拷贝文件 applicationContext.xml 到D:dir2dir1ssmchapter03ssmchapter03srcmainresources
正在从 D:dir1ssmchapter03ssmchapter03srcmainresources 拷贝文件 applicationContext1.xml 到D:dir2dir1ssmchapter03ssmchapter03srcmainresources
正在从 D:dir1ssmchapter03ssmchapter03srcmainresources 拷贝文件 applicationContext2.xml 到D:dir2dir1ssmchapter03ssmchapter03srcmainresources
正在从 D:dir1ssmchapter03ssmchapter03srcmainwebapp 拷贝文件 index.jsp 到D:dir2dir1ssmchapter03ssmchapter03srcmainwebapp
正在从 D:dir1ssmchapter03ssmchapter03srcmainwebappWEB-INF 拷贝文件 web.xml 到D:dir2dir1ssmchapter03ssmchapter03srcmainwebappWEB-INF
正在从 D:dir1ssmchapter03 拷贝文件 第3章 Spring AOP.pdf 到D:dir2dir1ssmchapter03
正在从 D:dir1 拷贝文件 ssmchapter03.rar 到D:dir2dir1
正在从 D:dir1 拷贝文件 ssmchapter04.rar 到D:dir2dir1
拷贝成功!


文件夹dir2情况

img

成功拷贝!


代码编译器:IntelliJ IDEA 2020.1

Archives QR Code Tip
QR Code for this page
Tipping QR Code