flower_wa 2021-10-26 00:45 采纳率: 100%
浏览 20
已结题

IO流复制一个多级文件夹

完成需求:使用IO流复制一个多级文件夹到目标路径
但是一致显示:路径找不到报错


public class CopyUp {

    public static void main(String[] args) throws IOException {
        File src = new File("D:\\SRP1");
        File target = new File("D:\\Dev\\7");

        copyAll(src,target);
    }

    //复制文件中的单个文件,
    public static void copyFile(File srcFile, File target) throws IOException {

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target));

        byte[] bys = new byte[1024];
        int len;
        while((len = bis.read(bys))!=-1){
            bos.write(bys);
        }

        bis.close();
        bos.close();

    }

    public static void copyAll(File src, File target) throws IOException {

        if (src.isDirectory()) {
            //在目标目录下创建名字相同的目录对象
            File newFolder = new File(target, src.getName());

            //判断目标真实目录是否存在,不存在就创建
            if (!newFolder.exists()) {
                newFolder.mkdir();
            }

            //遍历源目录下的所有文件或者目录
            File[] files = src.listFiles();

            for (File file : files) {
                copyAll(file, newFolder);
            }
        }else {
            File newFile = new File(target, src.getName());
            System.out.println(newFile.getAbsoluteFile());
            newFile.createNewFile();
            copyFile(src,newFile);
        }
    }


}


  • 写回答

1条回答 默认 最新

  • 水晶心泉 2021-10-26 07:51
    关注

    粗略看了一下,先把36行的mkdir改成mkdirs试下呢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月5日
  • 已采纳回答 10月28日
  • 创建了问题 10月26日

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分