前因:eclipse设置“User libraries “, 如果jar包分散在多个不同的目录,需要一个目录一个目录地选择,前天在设置一个项目要导入的jar包时就碰到这个问题,当时是直接从另一个同事那里拷了过来的,事后想了一个,当时应该写个小程序,实现从指定目录里面的子目录拷贝指定的文件到另一个指定目。刚好在接触Groovy,就抽时间写了一个小程序,实现了该功能。
源代码(功能基本实现,可能会存在不足):
class ExtrackJar2OneFolder {
def path;
def des;
def isReclusive = false;
def extrackJar(){
File fPath = new File(path);
File fDes = new File(des);
if(!fPath.isDirectory()){
println "Stop Processing...";
}
List fileList = fPath.listFiles();
for(File f : fileList){
if(f.isDirectory()){
// support path reclusive
if(isReclusive){
this.path = f.getPath();
extrackJar();
}
}else{
copy2Path(f);
}
}
}
def copy2Path(file){
if(file.getName().toUpperCase().lastIndexOf("JAR")!=-1 || file.getName().lastIndexOf("ZIP")!=-1){
def time=new Date().getTime();
def length=1024;
def fin=new FileInputStream(file);
def desFile = new File(des+"\\"+file.getName());
def fout=new FileOutputStream(desFile);
byte[] buffer=new byte[length];
while(true){
def ins=fin.read(buffer);
if(ins==-1){
fin.close();
fout.flush();
fout.close();
return new Date().getTime()-time;
}else
fout.write(buffer,0,ins);
}
}
}
public static void main(def args){
println "Mission Processing..."
ExtrackJar2OneFolder ejf = new ExtrackJar2OneFolder(["path":"D:\\from", "des":"d:\\test", "isReclusive":true]);
ejf.extrackJar();
println "Mission Over..."
}
}