zoukankan      html  css  js  c++  java
  • java-通过IO流复制文件夹到指定目录

    public class copyDirectoryDemo {
    
        public static void main(String[] args) {
            File srcFolder = new File("C:\Users\MA\Desktop\IOtest");
            File destFolder = new File("C:\Users\MA\Desktop\IOtest\test");
            fun(srcFolder, destFolder);
        }
    
        public static void fun(File srcFolder, File destFolder) {
            File[] fileArray = srcFolder.listFiles();
            if (!destFolder.exists()) {
                destFolder.mkdir();
            }
            for (File file : fileArray) {
                if (file.isDirectory()) {
                    String folderName = file.getName();
                    File newDestFolder = new File(destFolder, folderName);
                    fun(file, newDestFolder);
                } else {
                    String fileName = file.getName();
                    File destFile = new File(destFolder, fileName);
                    copy(file, destFile);
                }
            }
        }
    
        public static void copy(File file, File destFile) {
            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
            try {
                bis = new BufferedInputStream(new FileInputStream(file));
                bos = new BufferedOutputStream(new FileOutputStream(destFile));
                byte[] bys = new byte[1024];
                int len = 0;
                while((len=bis.read(bys))!=-1){
                    bos.write(bys,0,len);
                    
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }catch (IOException e){
                e.printStackTrace();
            }finally{
                if(bis!=null){
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(bos!=null){
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                
            }    
        }
    }
  • 相关阅读:
    通过构造函数检查生成对象个数
    动手动脑二
    产生随机数的几种方法
    素数输出
    递归实现回文串
    java的方法重载
    统计单词频率
    四则运算和随机验证码
    微信小程序--家庭记账本开发--04
    微信小程序--家庭记账本开发--03
  • 原文地址:https://www.cnblogs.com/mxj961116/p/9348742.html
Copyright © 2011-2022 走看看