package developers.utils;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;
/**
* @author zhumengjun
* @date 2020-06-14 21:32
* @desc 將修改過的文件Copy出來
* 運行前確認三處:compareDate distRoot rootMap
*
*/
public class NewFileCopyUtils {
private static DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final String javaSuffix = ".java";
private static final String classSuffix = ".class";
/** 從什麼時間之後開始修改的 */
private static String compareDate = "2020-06-11 09:30:07";
/** Copy文件和列表文件要保存到哪裏 */
private static String distRoot = "E:/MyNewFile";
/** 文件路徑列表要保存到哪裏 */
private static String pathFile;
/** 允許的文件後綴類型 */
private static List<String> allowSuffixList = new ArrayList<String>();
/** 路徑白名單 */
private static List<String> whiteList = new ArrayList<String>();
/** 工作空間路徑 */
private static String workSpacePath;
/** 存放源文件路徑的Map */
private static Map<String, String> rootMap = new HashMap<String, String>();
static {
rootMap.put(javaSuffix, "/iTreasuryEjb/src");//java源文件位置
rootMap.put(classSuffix, "/iTreasuryEjb/classes");//class源文件位置
rootMap.put("ebank.jsp", "/ebank/webapp");//下面這幾個key暫時沒有實際意義,只有value起作用
rootMap.put("iTreasuryWeb.jsp", "/iTreasuryWeb/webapp");
rootMap.put(".js", "/webdocs/WebRoot");
whiteList.addAll(rootMap.values());
allowSuffixList.add(javaSuffix);
allowSuffixList.add(classSuffix);
allowSuffixList.add(".jsp");
allowSuffixList.add(".js");
allowSuffixList.add(".html");
allowSuffixList.add(".xml");
if (workSpacePath == null) workSpacePath = getWorkSpacePath();
if (pathFile == null) pathFile = distRoot + "/CopyFile"+System.currentTimeMillis()+".txt";
}
public static void main(String[] args) {
try {
List<String> resultAll = getAllPathsByRootMap();
StringBuffer resultPaths = new StringBuffer();
if (resultAll.size() > 0) {
copyFileByPaths(resultAll);
for (String str : resultAll) {
if (str.endsWith(classSuffix)) continue;
if (str.indexOf("/developers/utils") > 0) continue;
resultPaths.append(str).append("\n");
}
write2File(resultPaths.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void write2File(String data) throws IOException{
FileWriter writer = null;
writer = new FileWriter(pathFile);
writer.write(data);
writer.close();
}
/**
* 根據rootMap獲取所有路徑
* @return
*/
private static List<String> getAllPathsByRootMap(){
List<String> resultAll = new ArrayList<String>();
File file = new File(workSpacePath+rootMap.get(javaSuffix));
ergodicDirectory(file, resultAll);
if (resultAll.size() > 0) {
Map<String,String> map = new HashMap<String, String>();
for (String path : resultAll) {
path = path.substring(path.indexOf(rootMap.get(javaSuffix))).replaceFirst(rootMap.get(".java"), "");
map.put(path.substring(0,path.indexOf(".")), javaSuffix);
}
file = new File(workSpacePath+rootMap.get(classSuffix));
List<String> resultClass = new ArrayList<String>();
ergodicDirectory(file, resultClass);
for (String classPath : resultClass) {
String tempPath = classPath.substring(classPath.indexOf(rootMap.get(classSuffix))).replaceFirst(rootMap.get(classSuffix), "");
if (tempPath.indexOf("$") > 0) {
tempPath = tempPath.substring(0,tempPath.indexOf("$"));
} else {
tempPath = tempPath.substring(0,tempPath.indexOf("."));
}
if (javaSuffix.equals(map.get(tempPath))) {
resultAll.add(classPath);
}
}
}
for(Map.Entry<String, String> entry : rootMap.entrySet()) {
if (entry.getKey().equals(javaSuffix) || entry.getKey().equals(classSuffix)) continue;
file = new File(workSpacePath+entry.getValue());
ergodicDirectory(file, resultAll);
}
return resultAll;
}
/**
* 遍歷查找符合條件的路徑
* @param file
* @param resultAll
* @param returnDirectory
* @return
*/
private static void ergodicDirectory(File file, List<String> resultAll) {
if (file == null) return;
File[] files = file.listFiles();
if (files == null || files.length <= 0) return;// 判斷目錄下是不是空的
for (File f : files) {
if (f.isDirectory()) {
ergodicDirectory(f, resultAll);
} else {
String filePath = f.getAbsolutePath();
if (fileSuffixValid(filePath)) {
filePath = filePath.replace("\\", "/");
if (dateValid(f)) {
if (pathWhiteListValid(filePath)) {
resultAll.add(filePath);
}
}
}
}
}
}
/**
* 根據路徑List複製出來文件
*/
private static void copyFileByPaths(List<String> pathList) {
StringBuffer successPath = new StringBuffer();
StringBuffer failPath = new StringBuffer();
try {
String tempPath = null;
for (String path : pathList) {
try {
tempPath = path.replaceFirst(workSpacePath, "");
FileUtils.copyFile(new File(path),new File(distRoot + tempPath));
successPath.append(path).append("\n");
} catch (Exception e) {
failPath.append(path).append("\n");
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("****************SUCCESS******************");
System.out.println(successPath.toString());
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("****************FAIL******************");
System.out.println(failPath.toString());
}
private static boolean fileSuffixValid(String path) {
if (allowSuffixList == null || allowSuffixList.size() <= 0) return true;
for (String type : allowSuffixList) {
if (path.endsWith(type) || path.endsWith(type.toUpperCase())) {
return true;
}
}
return false;
}
private static boolean pathWhiteListValid(String path) {
if (whiteList == null || whiteList.size() <= 0) return false;
for (String white : whiteList) {
if (path.indexOf(white) > -1) {
return true;
}
}
return false;
}
/**
* 如果文件日期晚於比較日期,則爲最近修改過的文件
* @param file
* @return
*/
private static boolean dateValid(File file) {
Date d = new Date(file.lastModified());
String format = df.format(d);
if (compareDate.compareTo(format) < 0) {
return true;
}
return false;
}
private static String getWorkSpacePath(){
String workspacePath = null;
try {
File directory = new File("");
workspacePath = directory.getCanonicalPath().replace("\\", "/");
workspacePath = workspacePath.substring(0,workspacePath.lastIndexOf("/"));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("工作空間路徑 = " + workspacePath);
return workspacePath;
}
}