import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.net.URL; import java.util.HashMap; import java.util.Map; public class FileRobert { private static FileRobert robert = new FileRobert(); private static int parentFloaderCount = 1; public void digPath(String path, Map<String,FileWrapper> callBackMap){ File file = new File(path); if(file.isFile()){ String location = file.getAbsolutePath(); String key = FileWrapper.extractKey(location); if(callBackMap.containsKey(key)){ FileWrapper wrapper = callBackMap.get(key); wrapper.appendNext(new FileWrapper(file.getAbsolutePath())); }else{ FileWrapper wrapper = new FileWrapper(file.getAbsolutePath()); if(parentFloaderCount>1){ wrapper.apendPrefix(); } callBackMap.put(key,wrapper); } }else{ File flist[] = file.listFiles(); for(File subFile :flist){ String subPath = subFile.getAbsolutePath(); digPath(subPath,callBackMap); } } } public static void main(String[] args) throws Exception { URL url = FileRobert.class.getProtectionDomain().getCodeSource().getLocation(); System.out.println(url.getPath()); File newFile ; FileWriter write ; BufferedWriter bufferedWriter; if(args.length==3){ String path = args[0];//"D:/UAT/AP"; String path2 = args[1];//"D:/UAT/AP2"; newFile = new File(args[2]+"\\log.csv"); write = new FileWriter(newFile,true); bufferedWriter = new BufferedWriter(write); if(newFile.exists()){ newFile.delete(); }else{ newFile.createNewFile(); } Map<String,FileWrapper> callBackMap = new HashMap<String,FileWrapper>(); robert.digPath(path, callBackMap); parentFloaderCount++; robert.digPath(path2, callBackMap); bufferedWriter.write("KEY,FILE1,SIZE,FILE2,SIZE"); bufferedWriter.newLine(); for(String key :callBackMap.keySet()){ String data = callBackMap.get(key).toString(); bufferedWriter.write(data); bufferedWriter.newLine(); } bufferedWriter.flush(); write.close(); bufferedWriter.close(); }else{ System.out.println("please input parm1=path1,parm2=path2,param3=csv file path"); Thread.sleep(2000); } } }
import java.io.File; public class FileWrapper { private final static String REPLACEMENT_STR = "*"; private final static String REPLACEMENT_PATTERN = "0{1,}\\d{2}"; private String fileWrapperName; private String location; private String fileSimpleName; private long size; private int page; private String next=""; private String prefix=""; @Override public String toString() { // TODO Auto-generated method stub return this.getFileWrapperName()+"," +prefix +this.getLocation()+"," +this.getSize()+"," +this.next+","; } public FileWrapper(String filePath) { super(); this.location = filePath; //System.out.println(filePath); File file = new File(filePath); this.setSize(file.length()); setKey(); } public String getKey(){ return this.getFileWrapperName(); } public void setKey(){ String location = this.getLocation(); String key = extractKey(location); this.setFileWrapperName(key); } public static String extractKey(String location) { String strArray[] = location.split("\\\\"); StringBuffer sb = new StringBuffer(); int startIdx = 0; for(String s :strArray){ startIdx++; if(startIdx>3){ sb.append("\\"+s); } } return sb.toString().replaceAll(REPLACEMENT_PATTERN, REPLACEMENT_STR); } public String getFileWrapperName() { return fileWrapperName; } public void setFileWrapperName(String fileWrapperName) { this.fileWrapperName = fileWrapperName; } public String getLocation() { return location; } public void setLocation(String filePath) { this.location = filePath; } public String getFileSimpleName() { return fileSimpleName; } public void setFileSimpleName(String fileSimpleName) { this.fileSimpleName = fileSimpleName; } public long getSize() { return size; } public void setSize(long size) { this.size = size; } public int getPage() { return page; } public void setPage(int page) { this.page = page; } public void appendNext( FileWrapper wrapper) { // TODO Auto-generated method stub next = wrapper.getLocation()+","+wrapper.getSize(); } public void apendPrefix() { // TODO Auto-generated method stub prefix+=",,"; } }