复制a文件夹的文件到b文件夹

    public Map testIn() throws Exception {
        Map res = new HashMap<>();;
        FileInputStream in = null;
        FileOutputStream out = null;

        String oldPath = "E:\\123";
        File fuFile = new File(oldPath);
        String[] list = fuFile.list();// 文件夹
        for (int i = 0; i < list.length; i++) {
            String fileName = list[i];
            String newPath = "E:\\newPaht\\"+fileName;
            String count = goodsService.getCountBySupplierGoodsId(fileName);// 和数据库中已有数据进行匹配(可无)
            if (!isEmpty(count) && Integer.parseInt(count) > 0){
                if (!(new File(newPath)).exists()) {
                    (new File(newPath)).mkdir();
                }
                String wjPath = oldPath+"\\"+fileName;// 图片所在文件夹
                File wjFile = new File(wjPath);
                String[] wjName = wjFile.list();// 文件夹
                for (int wji = 0; wji < wjName.length; wji++) {
                    String wj = wjName[wji];

                    File file = new File(newPath+"\\"+wj);
                    in = new FileInputStream(oldPath+"\\"+fileName+"\\"+wj);
                    out = new FileOutputStream(file);

                    byte[] buffer=new byte[20971];
                    int readByte = 0;
                    while((readByte = in.read(buffer)) != -1){
                        out.write(buffer, 0, readByte);
                    }
                }

            }
        }

        in.close();
        out.close();
        return res;
    }

 

注:io是针对文件的,如果复制问加价的话   会抛出  路径:(拒绝访问)  错误

      路径需精确到文件名称

你可能感兴趣的:(复制a文件夹的文件到b文件夹)