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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 马到成功2014 中级黑马   /  2014-7-6 10:01  /  889 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

虽然花了点时间  不过还是基本可以复制了
// 列出一个文件夹所有文件的方法,来进行复制整个文件夹
        public static void showDir(File file,File file2) throws IOException {                        
                File[] files = file.listFiles();                        
                        for (int i = 0; i < files.length; i++) {        
                                                        
                                if (files.isDirectory()){                                                
                                        File temp=new File(file2+"\\"+files.getName());
                                        file2=temp;
                                        file2.mkdir();
                                        showDir(files,temp);
                                
                                }else{                                                
                                        File copy=new File(file2+"\\"+files.getName());
                                        toCopy(files.toString(),copy.getPath());
                                        if(!copy.exists())                                       
                                                copy.createNewFile();
                                }
                        }
                }
        /*
         * sourcePath:源文件地址
         * targetPath:目标文件地址
         * 此功能实现复制文件内容,从sourcePath复制到targetPath
         */
        public static void toCopy(String sourcePath,String targetPath){
                File sourceFile=new File(sourcePath);
                File targetFile=new File(targetPath);
               
                //缓冲区初始化,因为有考虑到不是纯文件,用字节流
                BufferedInputStream bis=null;
                BufferedOutputStream bos=null;
               
                try {
                        bis=new BufferedInputStream(new FileInputStream(sourceFile));
                        bos=new BufferedOutputStream(new FileOutputStream(targetFile));
                        byte[] bt=new byte[1024];
                        int len=0;
                        while((len=bis.read(bt))!=-1){
                                bos.write(bt, 0, len);
                                bos.flush();
                        }
                } catch (FileNotFoundException e) {
                        // TODO 自动生成的 catch 块
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO 自动生成的 catch 块
                        e.printStackTrace();
                }finally{
                        try{
                                if(bis!=null)
                                        bis.close();
                                if(bos!=null)
                                        bos.close();
                        }catch(IOException e){
                                e.printStackTrace();
                        }
                }
        }
public static void main(String[] args) {
                File file=new File("d:\\aa"); //源文件
                File fileCopy=new File("e:\\ab"); //目标文件
                if(!fileCopy.exists())//初次创建目标文件夹
                        fileCopy.mkdir();
                try {
                        showDir(file,fileCopy);
                } catch (IOException e) {
                        // TODO 自动生成的 catch 块
                        e.printStackTrace();
                }
        }

0 个回复

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