zoukankan      html  css  js  c++  java
  • java递归复制文件夹

    package com.haiyisoft.hyoaService;

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;


    @Controller
    @RequestMapping("/copy")
    public class CopyFolderController {


    //http://localhost:6011/hyoaservice/copy/exec.do
    @RequestMapping(value="/exec")
    @ResponseBody
    public void upload(String oldPath, String newPath) {

    copyFolder( "F://cc", "E://cc");

    }

    private void copyFolder(String oldPath, String newPath) {
    // TODO Auto-generated method stub
    try {
    (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹
    File a=new File(oldPath);
    String[] file=a.list();
    File temp=null;
    for (int i = 0; i < file.length; i++) {
    if(oldPath.endsWith(File.separator)){
    temp=new File(oldPath+file[i]);
    }
    else{
    temp=new File(oldPath+File.separator+file[i]);
    }

    if(temp.isFile()){
    BufferedInputStream input = new BufferedInputStream (new FileInputStream(temp));


    BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(newPath + "/" + (temp.getName()).toString()));
    byte[] bytes=new byte[1024];
    int len=0;
    while ( (len = input.read(bytes)) != -1) {
    output.write(bytes,0,len);

    }

    output.flush();
    input.close();
    }
    if(temp.isDirectory()){//如果是子文件夹
    copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);
    }
    }
    }
    catch (Exception e) {
    System.out.println("复制整个文件夹内容操作出错");
    e.printStackTrace();

    }

    }

    }

  • 相关阅读:
    react dva routerRedux 备忘
    vue实现tab切换
    ios手机弹出层上表单的操作,收起键盘焦点错乱的问题
    FLEX布局
    new Date在ios下的兼容bug
    大数据分析如何创建最佳的移动应用用户体验
    Django适合做大用户量的系统吗?
    用 Django 管理现有数据库
    djongo:Django和MongoDB连接器
    5分钟教你学会Django系统错误监控
  • 原文地址:https://www.cnblogs.com/zhangzhiqin/p/11081349.html
Copyright © 2011-2022 走看看