开发者社区 问答 正文

如何复制指定路径的文件夹到指定的路径下

在只能使用字节流ByteArrayInputStream和ByteArrayOutputStream的情况下,如何复制指定路劲的文件夹到指定的路径下?

展开
收起
蛮大人123 2016-06-08 17:32:22 2071 分享 版权
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
        /**
         * 复制一个目录及其子目录、文件到另外一个目录
         * @param src
         * @param dest
         * @throws IOException
         */
        private void copyFolder(File src, File dest) throws IOException {
            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);
                    // 递归复制
                    copyFolder(srcFile, destFile);
                }
            } else {
                InputStream in = new FileInputStream(src);
                OutputStream out = new FileOutputStream(dest);
    
                byte[] buffer = new byte[1024];
    
                int length;
                
                while ((length = in.read(buffer)) > 0) {
                    out.write(buffer, 0, length);
                }
                in.close();
                out.close();
            }
        }
    2019-07-17 19:32:26
    赞同 展开评论
问答地址: