工具:从一个文件夹中复制jar到另一个文件夹中

工具类:从一个文件夹中复制jar到另一个文件夹中

需要的小伙伴可以试一试,很爽哦,有时候真的很需要!

需求:当我们拿到一个maven项目时,而maven项目的jar包都是通过pom.xml文件管理的,此时我们需要maven中的jar(很多,目录很复杂,很难手动复制)

有两种方式:

传统方式1.找到本地maven存放的位置,挨个进去复制jar包;(尝试了一下,复制一半花费了1个小时)

便捷方式2. 使用java写个工具帮我们做这件事, 方便快捷   (20s搞定)

有了需求,就有了我这里的工具类,下面介绍一下:

使用方式很简单,在我的main方法中你只需要给两个参数:一个源文件夹的路径,一个目的文件夹的路径。运行即可完成所有jar复制。

采用的技术:java的传统I/O操作

注意事项:

1.目的文件夹路径必须先创建,在使用

源代码:

package cc.makalu.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 
 * @ClassName:downJarFromFile
 * @Package:cc.makalu.file
 * @Description:从文件中复制jar包
 * @author:ljl
 * @version:v0.1
 * @data:2018年1月16日 下午2:25:38 备注:
 */
public class downJarFromFile {

	public static void readFile(String resourcePath, String targetFile,String fileName) throws FileNotFoundException {
		//C:\Users\Administrator\.m2\repository\antlr\antlr\2.7.7\antlr-2.7.7.jar
		String targetPath = targetFile+"\\"+fileName;
		targetPath = targetPath.replaceAll("\\\\", "/");
		//System.out.println(targetPath); //F:\jar文件\antlr-2.7.7.jar
		resourcePath = resourcePath.replaceAll("\\\\", "/");
		//System.out.println(resourcePath);
		FileInputStream in = new FileInputStream(resourcePath);
		FileOutputStream out = new FileOutputStream(targetPath);
		byte[] buffer = new byte[2097152];
		try {
			while ((in.read(buffer)) != -1) {
				out.write(buffer);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static boolean copyJarFromFile(String resourceFile, String targetFile) throws FileNotFoundException {
		boolean flag = false;
		File file = new File(resourceFile);
		if (!file.isDirectory()) {
			String fileName = file.getName();
			String absolutepath = file.getAbsolutePath();
			System.out.println("文件");
			System.out.println("path=" + file.getPath());
			System.out.println("absolutepath=" + absolutepath);
			System.out.println("name=" + fileName);
			
			// 获得文件后缀名
			if (fileName.indexOf(".") != -1) {
				String xt = fileName.substring(fileName.indexOf("."), fileName.length());
				if (xt.contains(".jar") && xt.endsWith(".jar")) {
					readFile(absolutepath, targetFile,fileName);
				}
			}
		} else if (file.isDirectory()) {
			System.out.println("文件夹");
			String[] filelist = file.list();
			for (int i = 0; i < filelist.length; i++) {
				File readfile = new File(resourceFile + "\\" + filelist[i]);
				if (!readfile.isDirectory()) {
					String fileName = readfile.getName();
					String absolutepath = readfile.getAbsolutePath();
					System.out.println("path=" + readfile.getPath());
					System.out.println("absolutepath=" + readfile.getAbsolutePath());
					System.out.println("name=" + readfile.getName());
					// 获得文件后缀名
					if (fileName.lastIndexOf(".") != -1) {
						String xt = fileName.substring(fileName.indexOf("."), fileName.length());
						if (xt.contains(".jar") && xt.endsWith(".jar")) {
							readFile(absolutepath, targetFile, fileName);
						}
					}
				} else if (readfile.isDirectory()) {
					copyJarFromFile(resourceFile + "\\" + filelist[i], targetFile);
				}
			}
		}
		return flag;
	}

	public static void main(String[] args) {
		try {
			copyJarFromFile("C:\\Users\\Administrator\\.m2\\repository", "F:\\jar文件");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

}  

看看运行截图:

 

 

 最终将C:\\Users\\Administrator\\.m2\\repository目录中的所有的jar全部复制到了F:\\jar文件  目录中。

当比人苦恼一个小时才能完成的事情,你几十秒就搞定了!!!这种感觉很爽哦

 

posted @ 2018-01-18 17:24  懒得烧蛇吃  阅读(630)  评论(0编辑  收藏  举报