Groovy小应用-文件拷贝

本文介绍了一种使用Groovy脚本实现批量文件拷贝的方法,特别是在Eclipse中设置User Libraries时,针对多个目录下jar包的快速整理。通过Groovy程序,可以从指定目录及其子目录中复制特定文件到目标位置,简化了手动操作的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前因: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..."
	}
}


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值