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

java复制原路径下所有文件

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

java复制原路径下所有文件

@Test
    public void test02() throws FileNotFoundException {
        String dirUrl = "D:\pdf";
        String downLoadUrl = "E:\pdf";
        findAllFiles(dirUrl,downLoadUrl);
    }
    public void save(String url, String downLoadUrl) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(new File(url));
            fos = new FileOutputStream(new File(downLoadUrl));
            byte[] bytes = new byte[1024];
            int count = 0;
            while ((count = fis.read(bytes,0,bytes.length)) != -1){
                fos.write(bytes);
                fos.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
    public void findAllFiles(String dirUrl,String downLoadUrl){
        File file = new File(dirUrl);
        File[] listFiles = file.listFiles();
        for (File listFile : listFiles) {
            //获取当前目录名称
            String listFileName = listFile.getName();
            //现在的目录
            String newDownLoadUrl = downLoadUrl + "\" + listFileName;
            File fileDown = new File(newDownLoadUrl);
            if(listFile.isDirectory()){
                //判断目录是否存在
                if (!fileDown.exists()){
                    try {
                        fileDown.mkdir();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                findAllFiles(listFile.getAbsolutePath(),newDownLoadUrl);
            }else{
                System.out.println(fileDown.getAbsolutePath());
                save(listFile.getAbsolutePath().toString(),fileDown.getAbsolutePath().toString());
            }
        }
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/337777.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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