栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java使用IO复制文件夹下所有项目到另一个文件夹下

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java使用IO复制文件夹下所有项目到另一个文件夹下

import java.io.*;

public class IO {
    public static void main(String[] args) throws IOException{
        File src= new File("sorceFile\"); //源文件夹
        File desc = new File("targetFile"); //目标文件夹
        long start = System.currentTimeMillis();
        copyFile(src, desc); //复制文件夹
        long end = System.currentTimeMillis();
        System.out.println("复制文件夹耗时:" + (end - start) + "ms");
    }

    public static void copyFile(File src, File desc) throws IOException {
        if (src.isFile()) { //如果是文件
            FileInputStream fis = new FileInputStream(src);
            String path = desc.getAbsolutePath();
            FileOutputStream fos = new FileOutputStream(path);
            BufferedInputStream bis = new BufferedInputStream(fis);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            byte[] b = new byte[1024*1024];
            int len = 0;
            while ((len = bis.read(b)) != -1) {
                bos.write(b, 0, len);
            }
            bos.flush();
            bos.close();
            bis.close();
        }else { //如果是文件夹
            File[] files = src.listFiles(); //获取文件夹下的所有文件
            if (files.length == 0) { //如果文件夹为空
                String srcPath = src.getName(); //获取文件夹名称
                //拿到新文件夹的路径,新文件夹的路径为:目标文件夹的路径+文件夹名称
                String descPath = (desc.getAbsolutePath().endsWith("\") ? desc.getAbsolutePath() : desc.getAbsolutePath() + "\" + srcPath);
                File newFile = new File(descPath); //创建新文件夹
                if (!newFile.exists()) { //如果新文件夹不存在
                    newFile.mkdirs(); //创建新文件夹
                }
            } else { //如果文件夹不为空
                for(File f : files) { //遍历文件夹下的所有文件
                    String srcPath = f.getName(); //获取文件或文件夹的名称
                    String descPath = ""; //新文件夹的路径
                    if (f.isFile()) { //如果是文件
                        descPath = desc.getAbsolutePath() + "\" + f.getName(); //新文件夹的路径为:目标文件夹的路径+文件夹名称
                    }
                    if (f.isDirectory()) { //如果是文件夹
                        //拿到新文件夹的路径,新文件夹的路径为:目标文件夹的路径+文件夹名称
                        descPath = (desc.getAbsolutePath().endsWith("\") ? desc.getAbsolutePath() : desc.getAbsolutePath() + "\" + srcPath);
                    }
                    File newFile = new File(descPath); //创建新文件夹
                    if (f.isDirectory()) { //如果是文件夹
                        if (!newFile.exists()) { //如果新文件夹不存在
                            newFile.mkdirs(); //创建新文件夹
                        }
                    }
                    copyFile(f, newFile); //递归调用,此时拿到的newFile为目标路径,f为源路径
                }
            }
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/845032.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号