A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 航航啊 初级黑马   /  2019-8-1 15:54  /  719 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


最近学习了很多东西,输入流,输出流
public class CopyFoldersDemo {
    public static void main(String[] args) throws IOException {
        //创建数据源File对象,路径是E:\\itcast
        File srcFile = new File("E:\\itcast");
        //创建目的地File对象,路径是F:\\
        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); //F:\\itcast
            if(!newFolder.exists()) {
                newFolder.mkdir();
            }


            //获取数据源File下所有文件或者目录的File数组
            File[] fileArray = srcFile.listFiles();


            //遍历该File数组,得到每一个File对象
            for(File file : fileArray) {
                //把该File作为数据源File对象,递归调用复制文件夹的方法
                copyFolder(file,newFolder);
            }
        } else {
            //说明是文件,直接复制,用字节流
            File newFile = new File(destFile,srcFile.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[] bys = new byte[1024];
        int len;
        while ((len = bis.read(bys)) != -1) {
            bos.write(bys, 0, len);
        }


        bos.close();
        bis.close();
    }
}


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马