首页 > 其他 > 详细

文件夹复制

时间:2018-03-11 10:19:50      阅读:232      评论:0      收藏:0      [点我收藏+]
//文件夹复制
    public static boolean folderCopy(String oldName,String newName){
        File oldfile = new File(oldName);
        File newfile = new File(newName);

        if(!oldfile.exists())return false;
        if(!newfile.exists()){
            newfile.mkdirs();
        }

        for (String name:oldfile.list()){
            File file = new File(oldName+"/"+name);
            if(file.isDirectory()){
                File subFile = new File(newName,name);
                subFile.mkdir();
                folderCopy(oldName+"/"+name,newName+"/"+name);
            }else{
                fileCopy2BufferByte(oldName+"/"+name,newName+"/"+name);
            }
        }
        return true;
    }

    //字节流高效缓冲区文件复制
    public static void fileCopy2BufferByte(String oldFileName,String newFileName){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try{
            bis = new BufferedInputStream(new FileInputStream(oldFileName));
            bos = new BufferedOutputStream(new FileOutputStream(newFileName));
            byte[] bytes = new byte[1024];
            int len = -1;
            while ((len = bis.read(bytes))!=-1)bos.write(bytes,0,len);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(bis!=null)bis.close();
                if(bos!=null)bos.close();
            }catch (IOException ee){
                ee.printStackTrace();
            }
        }
    }

 

文件夹复制

原文:https://www.cnblogs.com/luoyunyong/p/8543167.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!