zoukankan      html  css  js  c++  java
  • Java 目录和文件的复制

    1.复制一个目录及其子目录、文件到另外一个目录

    //复制一个目录及其子目录、文件到另外一个目录

    public void copyFolder(File src, File dest) throws IOException {

      if (src.isDirectory()) {

      if (!dest.exists()) {

      dest.mkdir();

      }

      String files[] = src.list();

      for (String file : files) {

      File srcFile = new File(src, file);

      File destFile = new File(dest, file);

      // 递归复制

      copyFolder(srcFile, destFile);

      }

      } else {

      InputStream in = new FileInputStream(src);

      OutputStream out = new FileOutputStream(dest);

       

      byte[] buffer = new byte[1024];

       

      int length;

      while ((length = in.read(buffer)) > 0) {

      out.write(buffer, 0, length);

      }

      in.close();

      out.close();

      }

      }

    1. private void copyFolder(File src, File dest) throws IOException {
    2.  
      if (src.isDirectory()) {
    3.  
      if (!dest.exists()) {
    4.  
      dest.mkdir();
    5.  
      }
    6.  
      String files[] = src.list();
    7.  
      for (String file : files) {
    8.  
      File srcFile = new File(src, file);
    9.  
      File destFile = new File(dest, file);
    10.  
      // 递归复制
    11.  
      copyFolder(srcFile, destFile);
    12.  
      }
    13.  
      } else {
    14.  
      InputStream in = new FileInputStream(src);
    15.  
      OutputStream out = new FileOutputStream(dest);
    16.  
       
    17.  
      byte[] buffer = new byte[1024];
    18.  
       
    19.  
      int length;
    20.  
       
    21.  
      while ((length = in.read(buffer)) > 0) {
    22.  
      out.write(buffer, 0, length);
    23.  
      }
    24.  
      in.close();
    25.  
      out.close();
    26.  
      }
    27.  
      }
  • 相关阅读:
    jeecg+activemq之AjaxServlet+tomcat7
    odoo8.0 win7 64位 安装配置(补遗)
    odoo种种
    MySQL种种
    html种种
    jQuery种种
    freemarker种种
    jQuery ui autocomplete 与easyUI冲突解决办法(重命名ui的autocomplete 和menu部分)
    vue实现点击按钮下载图片
    VUE父子组件之间的传值,以及兄弟组件之间的传值
  • 原文地址:https://www.cnblogs.com/Firesun/p/9424607.html
Copyright © 2011-2022 走看看