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) {
//path1 test1.war
//path2 test.war
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
String path = files[i].getPath();
if(files[i].isDirectory()){
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);
}
try {
System.out.println(files[i].getAbsolutePath());
String t_folder_name = files[i].getPath();
System.out.println(t_folder_name.lastIndexOf("/"));
String f =t_folder_name.substring(0,t_folder_name.lastIndexOf("/"));
System.out.println(f);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 复制单个文件
* @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();
}
}
}