java 文件统计


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+=",,";
}




}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值