• Java复制文件和目录


    • 复制文件方法
    /**
    * 复制文件
    * 把源文件sourceFile复制到目标文件distFile中
    *
    * @param sourceFile
    * @param distFile
    * @return
    */
    public boolean copyFile(
           String sourceFile,
           String distFile
    ) {
       // 输入流
       BufferedInputStream bis = null;
       // 输出流
       BufferedOutputStream bos = null;
    
       try {
           // 输入流
           bis = new BufferedInputStream(new FileInputStream(sourceFile));
           // 输出流
           bos = new BufferedOutputStream(new FileOutputStream(distFile));
           int readLen;
           // --- 开始copy
           byte[] buf = new byte[8192];
           while ((readLen = bis.read(buf)) > -1) {
               bos.write(buf, 0, readLen);
           }
       } catch (IOException e) {
           e.printStackTrace();
           return false;
       } finally {
           // 关闭
           try {
               if (bis != null) {
                   bis.close();
               }
               if (bos != null) {
                   bos.close();
               }
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
       return true;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 目录复制方法
    /**
     * 复制目录
     * 把源目录oldDir中的内容复制到目标目录newDir中
     *
     * @param oldDir
     * @param newDir
     */
    public void copyDir(
            String oldDir,
            String newDir
    ) {
        // 检查目标文件夹newDir是否存在
        File newDirFile = new File(newDir);
        if (!newDirFile.exists()) {
            newDirFile.mkdirs();
        }
    
        // 旧目录文件
        File f1 = new File(oldDir);
        // 旧目录下的文件数组
        File[] files = f1.listFiles();
        // 定义新目标地址
        String newTarget;
        // 遍历文件数组
        for (File ff : files) {
            // 如果是文件,直接复制
            if (!ff.isDirectory()) {
                newTarget = newDir + File.separator + ff.getName();
                copyFile(ff.getAbsolutePath(), newTarget);
            }
            // 如果是文件夹,运用递归进行文件夹的复制
            else {
                // 递归复制
                copyDir(oldDir + File.separator + ff.getName(), newDir + File.separator + ff.getName());
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
  • 相关阅读:
    java-php-python-商院足球赛事管理计算机毕业设计
    哪种网站适合物理服务器
    mysql中binlog日志
    Jmeter性能测试基础系列❤
    js逆向基础篇-某电商网站-xx街
    微信小程序云开发数据懒加载+打破云数据库返回数据条数限制
    【C++游戏引擎Easy2D】基于Node节点展开的Text文本拓展详解
    【Python】FastAPI 项目创建 与 Docker 部署
    WPF提供了哪些不同类型的画刷
    八、创建JWT工具类
  • 原文地址:https://blog.csdn.net/tu_wer/article/details/127411697