欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-04-03 22:37:35
...

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

视频演示

dir1和dir2

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

dir1中的文件和子文件夹

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

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

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

代码实现

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:\dir2\dir1
正在从 D:\dir1 拷贝文件 idea使用教程2017-06-01.pdf 到D:\dir2\dir1
正在从 D:\dir1 拷贝文件 ssmchapter01.rar 到D:\dir2\dir1
正在从 D:\dir1\ssmchapter02\ssmchapter02 拷贝文件 pom.xml 到D:\dir2\dir1\ssmchapter02\ssmchapter02
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\annotation 拷贝文件 AnnotationAssembleTest.java 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\annotation
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\annotation 拷贝文件 UserController.java 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\annotation
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\annotation 拷贝文件 UserDao.java 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\annotation
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\annotation 拷贝文件 UserDaoImpl.java 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\annotation
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\annotation 拷贝文件 UserService.java 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\annotation
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\annotation 拷贝文件 UserServiceImpl.java 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\annotation
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\assemble 拷贝文件 User.java 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\assemble
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\assemble 拷贝文件 XmlBeanAssembleTest.java 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\assemble
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\instance\constructor 拷贝文件 Bean1.java 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\instance\constructor
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\instance\constructor 拷贝文件 InstanceTest1.java 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\instance\constructor
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\instance\factory 拷贝文件 Bean3.java 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\instance\factory
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\instance\factory 拷贝文件 InstanceTest3.java 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\instance\factory
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\instance\factory 拷贝文件 MyBean3Factory.java 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\instance\factory
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\instance\static_factory 拷贝文件 Bean2.java 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\instance\static_factory
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\instance\static_factory 拷贝文件 InstanceTest2.java 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\instance\static_factory
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\instance\static_factory 拷贝文件 MyBean2Factory.java 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\instance\static_factory
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\scope 拷贝文件 Scope.java 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\scope
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\scope 拷贝文件 ScopeTest.java 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\Java\com\ssm\scope
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\resources 拷贝文件 beans1.xml 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\resources
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\resources 拷贝文件 beans2.xml 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\resources
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\resources 拷贝文件 beans3.xml 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\resources
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\resources 拷贝文件 beans4.xml 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\resources
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\resources 拷贝文件 beans5.xml 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\resources
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\resources 拷贝文件 beans6.xml 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\resources
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\webapp 拷贝文件 index.jsp 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\webapp
正在从 D:\dir1\ssmchapter02\ssmchapter02\src\main\webapp\WEB-INF 拷贝文件 web.xml 到D:\dir2\dir1\ssmchapter02\ssmchapter02\src\main\webapp\WEB-INF
正在从 D:\dir1\ssmchapter02 拷贝文件 第2章 Spring中的Bean.pdf 到D:\dir2\dir1\ssmchapter02
正在从 D:\dir1 拷贝文件 ssmchapter02.rar 到D:\dir2\dir1
正在从 D:\dir1\ssmchapter03\ssmchapter03 拷贝文件 pom.xml 到D:\dir2\dir1\ssmchapter03\ssmchapter03
正在从 D:\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\aspect 拷贝文件 MyAspect.java 到D:\dir2\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\aspect
正在从 D:\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\aspectj\annotation 拷贝文件 MyAspect.java 到D:\dir2\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\aspectj\annotation
正在从 D:\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\aspectj\annotation 拷贝文件 TestAnnotationAspectj.java 到D:\dir2\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\aspectj\annotation
正在从 D:\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\aspectj\xml 拷贝文件 MyAspect.java 到D:\dir2\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\aspectj\xml
正在从 D:\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\aspectj\xml 拷贝文件 TestXmlAspectj.java 到D:\dir2\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\aspectj\xml
正在从 D:\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\cglib 拷贝文件 CglibProxy.java 到D:\dir2\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\cglib
正在从 D:\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\cglib 拷贝文件 CglibTest.java 到D:\dir2\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\cglib
正在从 D:\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\cglib 拷贝文件 UserDao.java 到D:\dir2\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\cglib
正在从 D:\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\factorybean 拷贝文件 MyAspect.java 到D:\dir2\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\factorybean
正在从 D:\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\factorybean 拷贝文件 ProxyFactoryBeanTest.java 到D:\dir2\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\factorybean
正在从 D:\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\jdk 拷贝文件 JdkProxy.java 到D:\dir2\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\jdk
正在从 D:\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\jdk 拷贝文件 JdkTest.java 到D:\dir2\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\jdk
正在从 D:\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\jdk 拷贝文件 UserDao.java 到D:\dir2\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\jdk
正在从 D:\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\jdk 拷贝文件 UserDaoImpl.java 到D:\dir2\dir1\ssmchapter03\ssmchapter03\src\main\Java\com\ssm\jdk
正在从 D:\dir1\ssmchapter03\ssmchapter03\src\main\resources 拷贝文件 applicationContext.xml 到D:\dir2\dir1\ssmchapter03\ssmchapter03\src\main\resources
正在从 D:\dir1\ssmchapter03\ssmchapter03\src\main\resources 拷贝文件 applicationContext1.xml 到D:\dir2\dir1\ssmchapter03\ssmchapter03\src\main\resources
正在从 D:\dir1\ssmchapter03\ssmchapter03\src\main\resources 拷贝文件 applicationContext2.xml 到D:\dir2\dir1\ssmchapter03\ssmchapter03\src\main\resources
正在从 D:\dir1\ssmchapter03\ssmchapter03\src\main\webapp 拷贝文件 index.jsp 到D:\dir2\dir1\ssmchapter03\ssmchapter03\src\main\webapp
正在从 D:\dir1\ssmchapter03\ssmchapter03\src\main\webapp\WEB-INF 拷贝文件 web.xml 到D:\dir2\dir1\ssmchapter03\ssmchapter03\src\main\webapp\WEB-INF
正在从 D:\dir1\ssmchapter03 拷贝文件 第3章 Spring AOP.pdf 到D:\dir2\dir1\ssmchapter03
正在从 D:\dir1 拷贝文件 ssmchapter03.rar 到D:\dir2\dir1
正在从 D:\dir1 拷贝文件 ssmchapter04.rar 到D:\dir2\dir1
拷贝成功!


文件夹dir2情况

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

成功拷贝!


代码编译器:IntelliJ IDEA 2020.1