Java 多文件夹合并

来源:互联网 发布:网络稳定性测试软件app 编辑:程序博客网 时间:2024/06/05 20:46

出处:点击打开链接


[java] view plain copy
  1. package com.xx.test.copy;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileNotFoundException;  
  8. import java.io.FileOutputStream;  
  9. import java.io.IOException;  
  10.   
  11. public class TestCombineDirectory {  
  12.   
  13.     /** 
  14.      * @param args 
  15.      */  
  16.     public static void main(String[] args) {  
  17.           
  18.           
  19.         String[] sourceDirNames = {"D:/LuceneEx","D:/LuceneEx2"};  
  20.         String targetDirName = "D:/LuceneEx_backup";  
  21.           
  22.         combineDirectory(sourceDirNames,targetDirName);  
  23.     }  
  24.   
  25.     /** 
  26.      * 合并多个文件夹到一个文件夹中 
  27.      * @param sourceDirNames 
  28.      * @param targetDirName 
  29.      */  
  30.     private static void combineDirectory(String[] sourceDirNames,String targetDirName) {  
  31.           
  32.         if(sourceDirNames==null || sourceDirNames.length==0){  
  33.             throw new RuntimeException("待合并的文件夹不存在...");  
  34.         }  
  35.           
  36.         for(int i=0;i<sourceDirNames.length;i++){  
  37.               
  38.             copyDir(sourceDirNames[i], targetDirName);  
  39.         }  
  40.         System.out.println("合并所有的文件夹完成...");  
  41.     }  
  42.   
  43.     /** 
  44.      * 拷贝目录,递归的方法 
  45.      * @param sourceDirName 
  46.      * @param targetDirName 
  47.      */  
  48.     public static void copyDir(String sourceDirName, String targetDirName) {  
  49.           
  50.         File sourceDir = new File(sourceDirName);  
  51.         File targetDir = new File(targetDirName);  
  52.           
  53.         if(sourceDir==null || !sourceDir.exists()){  
  54.             throw new RuntimeException("待拷贝的文件夹不存在..."+sourceDir.getAbsolutePath());  
  55.         }  
  56.           
  57.         if(!sourceDir.isDirectory()){  
  58.             throw new RuntimeException("待拷贝的文件不是目录..."+sourceDir.getAbsolutePath());  
  59.         }  
  60.           
  61.         if(!targetDir.exists()){  
  62.             targetDir.mkdirs();  
  63.         }  
  64.           
  65.         File[] files = sourceDir.listFiles();  
  66.           
  67.         for(int i=0;files!=null && i<files.length;i++){  
  68.               
  69.             if(files[i].isFile()){  //复制文件  
  70.                   
  71.                 copyFile(files[i],new File(targetDirName+File.separator+files[i].getName()));  
  72.                   
  73.             }else if(files[i].isDirectory()){   //复制目录,递归的方法  
  74.                   
  75.                 // 复制目录     
  76.                 String dir1 = sourceDirName+File.separator+files[i].getName();    
  77.                 String dir2 = targetDirName+File.separator+files[i].getName();    
  78.                 copyDir(dir1, dir2);    
  79.             }  
  80.               
  81.         }  
  82.           
  83.         System.out.println("拷贝文件夹成功..."+sourceDir.getAbsolutePath());  
  84.     }  
  85.   
  86.     /** 
  87.      * 拷贝单个的文件 
  88.      * @param sourceFile    源文件 
  89.      * @param targetFile    目标文件 
  90.      */  
  91.     private static void copyFile(File sourceFile, File targetFile) {  
  92.         FileInputStream in = null;  
  93.         BufferedInputStream bis = null;  
  94.         FileOutputStream out = null;  
  95.         BufferedOutputStream bos = null;  
  96.         try {  
  97.             // 新建文件输入流并对它进行缓冲     
  98.             in = new FileInputStream(sourceFile);    
  99.             bis = new BufferedInputStream(in);    
  100.     
  101.             // 新建文件输出流并对它进行缓冲     
  102.             out = new FileOutputStream(targetFile);    
  103.             bos = new BufferedOutputStream(out);    
  104.                 
  105.             // 缓冲数组     
  106.             byte[] b = new byte[1024 * 5];    
  107.             int len;    
  108.             while ((len =bis.read(b)) != -1) {    
  109.                 bos.write(b, 0, len);    
  110.             }    
  111.             // 刷新此缓冲的输出流     
  112.             bos.flush();    
  113.               
  114.         } catch (FileNotFoundException e) {  
  115.             e.printStackTrace();  
  116.         } catch (IOException e) {  
  117.             e.printStackTrace();  
  118.         }finally{  
  119.             try {  
  120.                 if(bos!=null)  
  121.                     bos.close();  
  122.             } catch (IOException e) {  
  123.                 e.printStackTrace();  
  124.             }  
  125.               
  126.             try {  
  127.                 if(bis!=null)  
  128.                     bis.close();  
  129.             } catch (IOException e) {  
  130.                 e.printStackTrace();  
  131.             }  
  132.         }  
  133.     }  
  134. }