增量源码工具类_老大帮忙写的

版权声明:by DongBao https://blog.csdn.net/aaaadong/article/details/80097282
package com.taihe.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 增量源码包生产类
 * @author lee
 */
public class GeneratePatchUtilV3 {
	
	//增量文件set
	private static Set<String> PATCH_SET = new HashSet<String>();
	
	//源文件根目录(根据实际修改)
	private static String SOURCE_PATH = "C:\\xiangmu";
	
	//目标文件根目录(根据实际修改)
	private static String TARGET_PATH = "D:\\Project\\patch\\";
	
	//增量文件配置文件
	private static String PATCH_FILE_NAME = "patch.txt";
	
	//增量文件总数
	private static int PATCH_COUNT = 0;
	 
	public static void main(String[] args) {
		buildPatchCode();
	}
	
	/**
	 * 生成增量源码
	 */
	public static void buildPatchCode(){
		
		 TARGET_PATH += getTargetDirName(); 
		
		 System.out.println("源码路径:" + SOURCE_PATH);
		 System.out.println("目标路径:" + TARGET_PATH);
		 
	     try {
	    	 //读取增量文件txt
	    	 readPatchFiles();
	    	 
	    	 //拷贝增量文件到目标目录
	    	 System.out.println("---------- 开始拷贝增量文件 ----------");
	    	 for(String patchFile : PATCH_SET){
	    		 String srcFileName = SOURCE_PATH + File.separator + patchFile;
	    		 String targetFileName = TARGET_PATH + File.separator + patchFile;
	    	     File srcFile = new File(srcFileName); //根据一个路径得到File对象  
	    	     //判断源文件是否存在  
	    	     if(srcFile.exists()){  
	    	    	 //检查如果是文件夹则执行文件夹拷贝
	    	    	 if(srcFile.isDirectory()){
	    	    		 copyDir(srcFileName, targetFileName);
	    	    	 }
	    	    	 else//非文件夹按照文件执行拷贝
	    	    	 {
	    	    		 copyFile(srcFileName, targetFileName,true);
	    	    	 }
	    	     }else{
	    	    	 System.out.println("不存在:" + srcFile.getPath());
	    	     }
	    	 }
	    	 System.out.println("---------- 结束拷贝增量文件 ----------");
	    	 System.out.println("增量文件数量:" + PATCH_COUNT);
	    	 
	     } catch (Exception e) {
	    	 e.printStackTrace();
	     }
	}
	
	
	/**
	  * 获取目标目录名称
	  * @return 目录名称
	  */
	 private static String getTargetDirName(){
		 SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
        String now = df.format(new Date());
        
		 String dirName = "ekp_v3_"+now;
		 
		 return dirName;
	 }
	 
	
	/**
	 * 读取增量文件txt
	 */
	public static void readPatchFiles(){
		/* 读取数据 */
	     try {
	    	 
	    	 System.out.println("---------- 开始读取增量文件 ----------");
	    	 
	    	 PATCH_SET.clear();
	    	 
	         BufferedReader br = new BufferedReader(new InputStreamReader(GeneratePatchUtilV3.class.getClassLoader().getResourceAsStream(PATCH_FILE_NAME), "UTF-8"));
	         String lineTxt = null;
	         while ((lineTxt = br.readLine()) != null) {
	             if(lineTxt!=null && lineTxt.trim().length() > 0){
	            	if(lineTxt.startsWith("/")){
	            		 System.out.println("已读取:"+lineTxt);
		            	 PATCH_SET.add(lineTxt.trim());
	            	}else {
	            		 System.out.println(lineTxt);
	            	}
	             }else{
	            	 System.out.println(lineTxt);
	             }
	         }
	         br.close();
	     } catch (Exception e) {
	         System.err.println("read errors :" + e);
	     }
	}
	
	
	/**
	 * 拷贝文件
	 * @param srcFileName 源文件全路径名称
	 * @param targetFileName 目标文件全路径名称
	 * @param overlay 若已存在是否覆盖
	 * @return 拷贝结果
	 */
	public static boolean copyFile(String srcFileName,String targetFileName,boolean overlay){  
		 File srcFile = new File(srcFileName); //根据一个路径得到File对象  
	     //判断源文件是否存在  
	     if(!srcFile.exists()){  
	         try{  
	             throw new Exception("源文件:"+srcFileName+"不存在!");  
	         }catch(Exception e){  
	             e.printStackTrace();//将异常内容存到日志文件中  
	         }  
	         return false;  
	     }else if(!srcFile.isFile()){//判断是不是一个文件  
	         try {  
	             throw new Exception("复制文件失败,源文件:"+srcFileName+"不是一个文件!");  
	         } catch (Exception e) {  
	           
	             e.printStackTrace();  
	         }  
	         return false;  
	           
	     }  
	     //判断目标文件是否存在  
	     File destFile = new File(targetFileName);//目标文件对象destFile  
	     if(destFile.exists()){  
	         //如果目标文件存在并允许覆盖  
	         if(overlay){  
	             //删除已经存在的目标文件  
	             new File(targetFileName).delete();  
	         }  
	         System.out.println("已覆盖:"+destFile.getPath());
	     }else{  
	    	 System.out.println("已复制:"+destFile.getPath());
	         //如果目标文件所在目录不存在,则创建 目录  
	         if(!destFile.getParentFile().exists()){  
	             //目标文件所在目录不存在  
	             //mkdirs():创建此抽象路径名指定的目录,包括所有必需但不存在的父目录  
	             if(!destFile.getParentFile().mkdirs()){  
	                 //复制文件失败:创建目标文件所在目录失败  
	                 return false;  
	             }  
	         }  
	     }  
	       
	     
	     //增量文件数量递增
	     PATCH_COUNT++;
	     
	     //复制文件  
	     int byteread = 0;//读取的字节数  
	     InputStream  in = null;  
	     OutputStream  out = null;  
	       
	     try {  
	         in  = new FileInputStream(srcFile);  
	         out = new FileOutputStream(destFile);  
	         byte[] buffer = new byte[1024];  
	         /* 
	          * 方法说明: 
	          * ①:将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。 
	          *      write(byte[] b, int off, int len)  
	          *          b - 数据 
	          *          off - 数据中的起始偏移量。 
	          *          len - 要写入的字节数。  
	          * ②:in.read(buffer))!=-1,是从流buffer中读取一个字节,当流结束的时候read返回-1 
	          */  
	         while((byteread = in.read(buffer))!=-1){  
	             out.write(buffer, 0, byteread);  
	         }  
	         return true;  
	     } catch (FileNotFoundException e) {  
	           
	         return false;  
	     } catch (IOException e) {  
	         return false;  
	     }finally{  
	         try {  
	             if(out!=null){  
	                 out.close();  
	             }  
	             if(in!=null){  
	                 in.close();  
	             }  
	         } catch (IOException e) {  
	               
	             e.printStackTrace();  
	         }  
	     }  
	 }
	
	 public static void copyDir(String sourceFileName, String targetFileName) throws IOException {
		File srcFile = new File(sourceFileName);
        String[] subFilePath = srcFile.list();
        if (!(new File(targetFileName)).exists()) {
            (new File(targetFileName)).mkdir();
        }
        
        for (int i = 0; i < subFilePath.length; i++) {
        	String  subFile = subFilePath[i];
            if ((new File(sourceFileName + File.separator + subFile)).isDirectory()) {
                copyDir(sourceFileName  + File.separator  + subFile, targetFileName  + File.separator + subFile);
            }
            if (new File(sourceFileName  + File.separator + subFile).isFile()) {
                copyFile(sourceFileName + File.separator + subFile, targetFileName + File.separator + subFile,true);
            }
        }
    }
}

根目录src下写一个patch.txt

内容如下

#增量文件夹
/WebContent/resource/
#增量js
/WebContent/resource/test.js
#增量jsp
/WebContent/resource/test.jsp
#增量java
/src/Test.java

右键运行run as java appapplication 即可



猜你喜欢

转载自blog.csdn.net/aaaadong/article/details/80097282