Java程序增量更新是一个吃力不讨好的工作,浪费了时间不说,还很容易出现漏掉文件、错放文件位置等问题。如果有一个比较好的管理机制,把这些事情交给程序自动处理就省事的多了。
工具尚不成熟,不过用起来比手动去选择增量文件要方便的多。下面就直接上代码了。
工具包括三个工具类,一个配置文件,一个程序启动类,具体如下:
文件工具类
package cn.lihua.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
/**
* 文件管理工具类
* @author lihua
* @version V1.0
* @createDate 2012-10-27
*/
public class FileUtil {
/**
* 复制文件
*
* @param srcFile
* 源文件File
* @param destDir
* 目标目录File
* @param newFileName
* 新文件名
* @return 实际复制的字节数,如果文件、目录不存在、文件为null或者发生IO异常,返回-1
*/
public static long copyFile1(File srcFile, File destDir, String newFileName) {
long copySizes = 0;
if (!srcFile.exists()) {
System.out.println("源文件不存在");
copySizes = -1;
} else if (!destDir.exists()) {
System.out.println("目标目录不存在");
copySizes = -1;
} else if (newFileName == null) {
System.out.println("文件名为null");
copySizes = -1;
} else {
try {
BufferedInputStream bin = new BufferedInputStream(
new FileInputStream(srcFile));
BufferedOutputStream bout = new BufferedOutputStream(
new FileOutputStream(new File(destDir, newFileName)));
int b = 0, i = 0;
while ((b = bin.read()) != -1) {
bout.write(b);
i++;
}
bout.flush();
bin.close();
bout.close();
copySizes = i;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return copySizes;
}
/**
* 复制文件(以超快的速度复制文件)
*
* @param srcFile
* 源文件File
* @param destDir
* 目标目录File
* @param newFileName
* 新文件名
* @return 实际复制的字节数,如果文件、目录不存在、文件为null或者发生IO异常,返回-1
*/
public static long copyFile2(File srcFile, File destDir) {
long copySizes = 0;
if (!srcFile.exists()) {
System.out.println("源文件不存在");
return -1;
}
try {
FileChannel fcin = new FileInputStream(srcFile).getChannel();
FileChannel fcout = new FileOutputStream(destDir).getChannel();
long size = fcin.size();
fcin.transferTo(0, fcin.size(), fcout);
fcin.close();
fcout.close();
copySizes = size;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return copySizes;
}
public static void copyDict(File source, File target,long last,String target_pack) {
File[] file = source.listFiles();
for (int i = 0; i < file.length; i++) {
if(file[i].getName().contains("svn")){
continue;
}
if (file[i].isFile()) {
if(file[i].lastModified()>=last){
if((file[i].getName().endsWith(".hbm.xml")||file[i].getName().endsWith(".class"))&&
notEmpty(target_pack)&&!source.getAbsolutePath().contains(target_pack)){
continue;
}
System.out.println(source.getAbsolutePath());
File sourceDemo = new File(source.getAbsolutePath() + "/"
+ file[i].getName());
File destDemo = new File(target.getAbsolutePath() + "/"
+ file[i].getName());
copyFile2(sourceDemo, destDemo);
}
}
if (file[i].isDirectory()) {
File sourceDemo = new File(source.getAbsolutePath() + "/"
+ file[i].getName());
File destDemo = new File(target.getAbsolutePath() + "/"
+ file[i].getName());
destDemo.mkdir();
copyDict(sourceDemo, destDemo,last,target_pack);
}
}
}
public static boolean notEmpty(String str) {
return str!=null&&!str.isEmpty();
}
/**
* 循环删除空的文件夹
* @param dir
*/
public static void deleteEmptyDir(File dir) {
if (dir.isDirectory()) {
File[] fs = dir.listFiles();
if (fs != null && fs.length > 0) {
for (int i = 0; i < fs.length; i++) {
File tmpFile = fs[i];
if (tmpFile.isDirectory()) {
deleteEmptyDir(tmpFile);
}
if (tmpFile.isDirectory() && tmpFile.listFiles().length <= 0) {
tmpFile.delete();
}
}
}
if (dir.isDirectory() && dir.listFiles().length == 0) {
dir.delete();
}
}
}
public static void main(String[] args) {
String [] s = "127.0.0.1 - - [14/Sep/2012:11:27:10 +0800] POST /newkyhb/sys/loginlognews.action HTTP/1.1 200 356 0.032".split(" ");
for (String string : s) {
System.out.println(string);
}
}
}
Properties文件工具类
package cn.lihua.util;
import java.io.InputStream;
import java.util.Properties;
/**
* Properties 文件读写工具类
* @author lihua
* @version V1.0
* @createDate 2012-10-27
*/
public class PropertiesUtil {
//private static final Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
private static final Properties prop = loadPropertiesFile("setting.properties");
private static Properties loadPropertiesFile(String filePath) {
InputStream in;
try {
in = PropertiesUtil.class.getClassLoader().getResourceAsStream(filePath);
Properties p = new Properties();
p.load(in);
return p;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String getString(String key) {
if (prop != null)
return prop.getProperty(key);
return null;
}
}
工程目录工具类
package cn.lihua.util;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
* 工程根目录工具类
* @author lihua
* @version V1.0
* @createDate 2012-9-13
*/
public class WebRootUtil {
private static Document doc = null;
/**
* 初始化操作
* @param xmlFile
* @throws Exception
*/
public static void init(String xmlFile) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(new File(xmlFile));
}
/**
* 获取.mymetadata中的web root folder 名称
* @param xmlFile
* @return
* @throws Exception
*/
public static String getRootName(String xmlFile) throws Exception {
String webRoot = "WebRoot";
init(xmlFile);
NodeList nodeList = doc.getElementsByTagName("attribute");
for (int i = 0, len = nodeList.getLength(); i < len; i++) {
Element attribute = (Element) nodeList.item(i);
if ("webrootdir".equals(attribute.getAttribute("name"))){
webRoot = attribute.getAttribute("value");
break;
}
}
return webRoot;
}
public static void main(String[] args)throws Exception {
System.out.println(getRootName("D:\\workspace\\newkyhb\\.mymetadata"));
}
}
Properties配置文件
#\u4E0A\u4F20\u6587\u4EF6\u5B58\u653E\u8DEF\u5F84 source_path=D\:\\workspace\\newkyhb target_path=D\:\\update_version update_date=2012-09-13 target_pack=\\jsp
启动类
package cn.lihua;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import cn.lihua.util.FileUtil;
import cn.lihua.util.PropertiesUtil;
import cn.lihua.util.WebRootUtil;
/**
* 启动入口类
* @author lihua
* @version V1.0
* @createDate 2012-10-27
*/
public class Startup {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
private static long packed_date;//修改文件起始时间
private static String source_path;//原工程目录
private static String target_path;//打包目录
private static String target_pack;//class打包包名
private static String project_new_name;//新打包工程名称
private static String meta_data_file =".mymetadata";//根目录配置文件
private static String root_name;//拷贝根目录
/**
* 初始化操作
* @throws Exception
*/
private static void init()throws Exception{
String update_date =PropertiesUtil.getString("update_date");
packed_date = sdf.parse(update_date).getTime();
source_path =PropertiesUtil.getString("source_path");
String project_name = source_path.substring(source_path.lastIndexOf("\\")+1);//原工程名
project_new_name = project_name + "(" + sdf.format(new Date()) + ")";
root_name = WebRootUtil.getRootName(source_path + File.separator + meta_data_file);
target_pack =PropertiesUtil.getString("target_pack");
target_path =PropertiesUtil.getString("target_path");
File target = new File(target_path + File.separator + project_new_name);
if(!target.exists()){
target.mkdirs();
}
}
/**
* 执行拷贝任务
*/
public static void copy(){
/**
* 拷贝文件及文件夹
*/
FileUtil.copyDict(
new File(source_path + File.separator + root_name),
new File(target_path + File.separator + project_new_name),
packed_date,target_pack);
/**
* 删除空文件夹
*/
FileUtil.deleteEmptyDir(
new File(target_path + File.separator + project_new_name));
}
/**
* 程序入口
* @param args
* @throws Exception
*/
public static void main(String[] args)throws Exception {
System.out.println("======================================");
System.out.println("正在进行初始化操作……");
init();
System.out.println("======================================");
System.out.println("拷贝的源文件目录如下:");
copy();
System.out.println("======================================");
System.out.println("打包完成! ");
}
}