加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Groovy小应用-文件拷贝

发布时间:2020-12-14 16:55:10 所属栏目:大数据 来源:网络整理
导读:前因:eclipse设置“User?libraries “, 如果jar包分散在多个不同的目录,需要一个目录一个目录地选择,前天在设置一个项目要导入的jar包时就碰到这个问题,当时是直接从另一个同事那里拷了过来的,事后想了一个,当时应该写个小程序,实现从指定目录里面的

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读