• 【JavaSE】【IO流】复制单级或多级文件夹


    复制单级文件夹

    需求

    把E:itcast这个文件夹复制到模块目录下

    代码

    import java.io.*;
    
    public class demo {
        public static void main(String[] args) throws IOException {
            //创建数据源目录File对象
            File srcFolder = new File("D:\下载");
    
            //获取数据源目录File对象的名称(itcase)
            String srcFolderName = srcFolder.getName();
    
            //创建目的地目录File对象,路径名是模块名+itcase组合
            File destFolder = new File("text",srcFolderName);
    
            //判断目的地目录对应的数据源File对象是否存在,不存在,则创建
            if(!destFolder.exists()){
                destFolder.mkdir();
            }
    
            //获取数据源目录下的所有File数组
            File[] listFiles = srcFolder.listFiles();
    
            //遍历File数组,得到每一个File对象,该File对象,其实就是数据源文件
            for(File srcFile:listFiles){
                //获取每一个File对象的名字
                String srcFileName = srcFile.getName();
                //创建目的地文件File对象
                File destFile = new File(destFolder,srcFileName);
                //复制文件
                copyFile(srcFile,destFile);
            }
    
        }
        //字节缓冲流
        private static void copyFile(File srcFile, File destFile) throws IOException{
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
            byte[] bytes = new byte[1024];
            int len;
            while ((len=bis.read(bytes))!=-1){
                bos.write(bytes,0,len);
            }
            bis.close();
            bos.close();
        }
    }
    

    复制多级文件夹

    需求

    把E:itcast这个文件夹复制到F:下

    代码

    import java.io.*;
    
    
    public class demo1 {
        public static void main(String[] args) throws IOException {
            //创建数据源File对象
            File srcFile = new File("E:\itcase");
    
            //创建目的地File对象
            File destFile = new File("F:\");
    
            //写方法实现文件夹的复制,参数为数据源File对象和目的地File对象
            copyFolder(srcFile,destFile);
        }
    
        //复制文件夹
        private static void copyFolder(File srcFile, File destFile) throws IOException {
            //判断数据源File是否为目录
            if(srcFile.isDirectory()){
                //在目的地下创建和数据源File名称一样的目录
                String srcFileName = srcFile.getName();
                File newFolder = new File(destFile,srcFileName);
                if(!newFolder.exists()){
                    newFolder.mkdir();
                }
    
                //获取数据源下的所有文件或者目录的数组
                File[] fileArray = srcFile.listFiles();
    
                //遍历该File数组,得到每一个File对象
                for(File file:fileArray){
                    copyFolder(file,newFolder);
                }
            }else {
                //说明是文件,直接复制,用字节流
                File newFile = new File(destFile,destFile.getName());
                copyFile(srcFile,newFile);
            }
        }
        //字节缓冲流
        private static void copyFile(File srcFile, File destFile) throws IOException{
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
            byte[] bytes = new byte[1024];
            int len;
            while ((len=bis.read(bytes))!=-1){
                bos.write(bytes,0,len);
            }
            bis.close();
            bos.close();
        }
    }
    
  • 相关阅读:
    elasticDump的安装使用
    centos7中给Elasticsearch5 安装bigdesk
    centos7下Elasticsearch5.2.2和head 插件环境搭建
    渗透测试入门DVWA 环境搭建
    windows环境下运行Elasticsearch
    UltraISO刻录CentOS 7安装指南
    TCP 协议中的 Window Size与吞吐量
    php 抛出异常
    php获取字符串长度
    php批量转换时间戳
  • 原文地址:https://www.cnblogs.com/mudongyousi/p/13277884.html
Copyright © 2020-2023  润新知