• Java实现文件夹复制


    使用递归复制文件夹和文件 

    package constxiong.interview;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    
    /**
     * 复制文件夹
     * @author Liangjw
     * @date 2020-4-6 15:00:51
     */
    public class TestCopyDir {
    
        public static void main(String[] args) {
            String srcPath = "E:/a";
            String destPath = "E:/b";
            copyDir(srcPath, destPath);
        }
        
        /**
         * 复制文件夹
         * @param srcFile
         * @param destFile
         */
        public static void copyDir(String srcDirPath, String destDirPath) {
            File srcDir = new File(srcDirPath);
            if (!srcDir.exists() || !srcDir.isDirectory()) {
                throw new IllegalArgumentException("参数错误");
            }
            File destDir = new File(destDirPath);
            if (!destDir.exists()) {
                destDir.mkdirs();
            }
            File[] files = srcDir.listFiles();
            for (File f : files) {
                if (f.isFile()) {
                    copyFile(f, new File(destDirPath, f.getName()));
                } else if (f.isDirectory()) {
                    copyDir(srcDirPath + File.separator + f.getName(),
                            destDirPath + File.separator + f.getName());
                }
            }
        }
        
        /**
         * 复制文件
         * @param srcFile
         * @param destFile
         */
        public static void copyFile(File srcFile, File destFile) {
            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
            byte[] b = new byte[1024];
            
            try {
                bis = new BufferedInputStream(new FileInputStream(srcFile));
                bos = new BufferedOutputStream(new FileOutputStream(destFile));
                int len;
                while ((len = bis.read(b)) > -1) {
                    bos.write(b, 0, len);
                }
                bos.flush();
            } 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();
                    }
                }
            }
        }
        
    }
  • 相关阅读:
    半年时间
    deep learning书的阅读
    wgan pytorch,pyvision, py-faster-rcnn等的安装使用
    caffe新版本的各种软件
    你会允许自己家孩子一直不停跟人要东西吗?
    sup inf max min
    leangoo大讲堂—北京站
    使用Leangoo玩转故事地图
    Leangoo:用敏捷开发管理思维做团队协作的SaaS软件
    张江男的逆袭,我如何使用leangoo提升团队效率
  • 原文地址:https://www.cnblogs.com/4AMLJW/p/InputStreamOutputStream20200406150209.html
Copyright © 2020-2023  润新知