1.创建Java项目,编写替换类文件:
package com.tp.update;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
public class UpdateFile {
private static String source;
private static String target;
public static void main(String[] args) {
source = args[0];
target = args[1];
getFile(source);
}
public static void getFile(String list_path){
File root = new File(list_path);
File[] files = root.listFiles();
for(int i=0;i<files.length;i++){
String path = files[i].getPath();
String child_path = path.substring(source.length());
String target_child_path = target+child_path;
if(files[i].isDirectory()){
//首先判断该文件夹在目标路径中是否已经存在
File f = new File(target_child_path);
if(f.exists()){
getFile(path);
}else{
f.mkdirs();
getFile(path);
}
}else{
//String child_path = path.substring(source.length());
//String target_child_path = target+child_path;
File target_child_file = new File(target_child_path);
if (target_child_file.exists()){
long c_date = target_child_file.lastModified();
String t_folder_name = target_child_file.getPath();
t_folder_name =t_folder_name.substring(0,t_folder_name.lastIndexOf("\\"));
String t_file_name = target_child_file.getName();
target_child_file.renameTo(new File(t_folder_name+"\\"+t_file_name+"."+c_date));
}
copyFile(path,target_child_path);
}
}
}
/**
* 复制单个文件
* @param oldPath String 原文件路径 如:c:/fqf.txt
* @param newPath String 复制后路径 如:f:/fqf.txt
* @return boolean
*/
public static void copyFile(String oldPath,String newPath){
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
}
2.右键项目----export----java----runnable jar file----打成jar包 updateProject.jar
3.编写启动项 start.bat,并和jar放在同一路径下
4.启动bat
附bat文件内容:
@echo off
D:\JDK\jdk1.7.0_60\bin\java.exe(指定jdk) -jar updateProject.jar F:\test\ceb1.war(替换文件路径) F:\test\ceb.war(被替换文件路径)