Tao-yangZeng 2015-07-20 00:07 采纳率: 60%
浏览 1604
已采纳

java目录复制的问题,最上层目录无法复制

public static void copyDir(File src, File dest) throws FileNotFoundException{
if(src.isDirectory()){
if(!dest.exists()){
dest.mkdir();
}

        String[] files = src.list();
        for (String file : files) {
            File srcFile = new File(src,file);
            File destFile = new File(dest,file);
            copyDir(srcFile, destFile);
        }
    }
    else{
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dest);
        byte[] buffer = new byte[1024];
        int length;
        try {
            while((length=in.read(buffer))!=-1){
                out.write(buffer, 0, length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                in.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

主函数

public static void main(String[] args) {

    String source = "E:\\aa";
    String dest = "D:\\dd";
    File src = new File(source);
    File des = new File(dest);


    try {
        FileUtils.copyDir(src, des);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}
复制目录 只能复制aa文件夹以下的文件夹和文件,怎么改?可以把aa也一起复制呢?
  • 写回答

2条回答 默认 最新

  • danielinbiti 2015-07-20 01:19
    关注
     public static void copyDir(File src, File dest,boolean isFirst) throws FileNotFoundException{
            if(src.isDirectory()){
                if(isFirst){
                    String s = src.getName();
                    dest = new File(dest,s);
                    if(!dest.exists()){
                        dest.mkdir();
                    }
                }
                if(!dest.exists()){
                    dest.mkdir();
                }
                    String[] files = src.list();
                    //String srcfileAbsoutPath =  src.getAbsolutePath();
                    //String destfileAbsoutPath =  dest.getAbsolutePath();
                    for (String file : files) {
                        File srcFile = new File(src,file);
                        File destFile = new File(dest,file);
                        copyDir(srcFile, destFile,false);
                    }
                }
                else{
                    InputStream in = new FileInputStream(src);
                    OutputStream out = new FileOutputStream(dest);
                    byte[] buffer = new byte[1024];
                    int length;
                    try {
                        while((length=in.read(buffer))!=-1){
                            out.write(buffer, 0, length);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally{
                        try {
                            in.close();
                            out.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
    
    
            public static void main(String[] args) {
            String source = "i:\\test8\\deltest";
            String dest = "D:\\dd";
            File src = new File(source);
            File des = new File(dest);
    
    
            try {
                FileUtils.copyDir(src, des,true);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我这模型写的不对吗?为什么lingo解出来的下面影子价格这一溜少一个变量
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波