Java复制一个目录及其子目录的文件到另外一个目录

来源:互联网 发布:实体店想在淘宝进货 编辑:程序博客网 时间:2024/05/10 05:46
  1. /** 
  2.  * 复制一个目录及其子目录的文件到另外一个目录 
  3.  */  
  4. private void copyFolder(File src, File dest) throws IOException {  
  5.     if (src.isDirectory()) {  
  6.         if (!dest.exists()) {  
  7.             dest.mkdir();  
  8.         }  
  9.         String files[] = src.list();  
  10.         for (String file : files) {  
  11.             File srcFile = new File(src, file);  
  12.             File destFile = new File(dest, file);  
  13.             // 递归
  14.             copyFolder(srcFile, destFile);  
  15.         }  
  16.     } else {  
  17.         InputStream in = new FileInputStream(src);  
  18.         OutputStream out = new FileOutputStream(dest);  
  19.   
  20.         byte[] buffer = new byte[1024];  
  21.         int length;  
  22.         while ((length = in.read(buffer)) > 0) {  
  23.             out.write(buffer, 0, length);  
  24.         }  
  25.         in.close();  
  26.         out.close();  
  27.     }  

阅读全文
0 0
原创粉丝点击