zoukankan      html  css  js  c++  java
  • File 类递归练习,拷贝。

    public class digui {
    /*


    * 需求3:从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
    *
    */
    public static void main(String[] args) throws Exception {
    File src = getDir();// 获取文件夹路径
    File desc = getDir();
    if (src.equals(desc)) {
    System.out.println("目标文件夹是源文件夹的子文件夹");
    } else {
    copy(src, desc);
    }
    }

    public static void copy(File src, File dest) throws Exception {
    // 1.在目标文件夹中创建需要拷贝的文件夹
    File newDir = new File(dest, src.getName());
    newDir.mkdir();
    // 2.获取被拷贝的文件夹中所有的文件和文件夹,存储在File数组中
    File[] subFile = src.listFiles();
    // 3.遍历
    for (File subFile1 : subFile) {
    if (subFile1.isFile()) { // 判断是文件
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(subFile1));
    BufferedOutputStream bos = new BufferedOutputStream(
    new FileOutputStream(new File(newDir, subFile1.getName())));
    int b;
    while ((b = bis.read()) != -1) {
    bos.write(b);
    }
    } else {
    copy(subFile1, newDir); // 如果是文件就递归调用
    }
    }
    }

    }

  • 相关阅读:
    油猴脚本
    js hook
    js控制台原理检测
    安卓so文件函数动态注册
    js一些简单逆向题目实战
    js基础补充落下的知识点
    js反爬原理
    js容易让人眼瞎的写法
    js基础
    js一些常见非指纹built-in函数
  • 原文地址:https://www.cnblogs.com/wangffeng293/p/13295851.html
Copyright © 2011-2022 走看看