xl_echo编辑整理,欢迎转载,转载请声明文章来源。更多IT、编程案例、资料请联系QQ:1280023003百战不败,依不自称常胜,百败不颓,依能奋力前行。——这才是真正的堪称强大!!
该功能的实现主要是便于maven项目或者web的jar引入,能够快速通过pom文件找到需要的jar包。指定要输出的路径之后,该功能会直接通过pom文件寻找repository,并复制。最终导入项目就不需要一个一个去网上下载了。功能还是比较简单的,直接上代码。
package com.echo;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class FileUtils {
public static void main(String[] args) throws Exception {
}
public static Set<String> method() throws DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("D:\\CodeWork\\myeclipse\\code1\\com-echo-basic\\pom.xml"));
Set<String> listName = new HashSet<>();
//获取文档根节点
Element root = document.getRootElement();
//获取根节点下面的所有子节点(不包过子节点的子节点)
List<Element> list = root.elements();
//遍历List的方法
for (Element e:list){
if(e.getName().equals("dependencies")){
List<Element> elements = e.elements();
for (Element e1 : elements){
if(e1.getName().equals("dependency")){
List<Element> elements1 = e1.elements();
StringBuffer sb = new StringBuffer();
for (Element e2 : elements1){
if(e2.getName().equals("artifactId")){
sb.append(e2.getData());
}
if(e2.getName().equals("version")){
sb.append("-").append(e2.getData());
}
listName.add(sb.toString()+".jar");
}
}
}
}
}
return listName;
}
public static void fileCopy() throws DocumentException {
List<File> getallfile = getallfile("D:\\develop\\apache-localRepository");
Set<String> method = method();
Integer num = 0;
for (File file : getallfile) {
if (file.getName().endsWith(".jar")) {
if(method.contains(file.getName())){
num ++;
copyFileReaderToWriterChar(file.getAbsolutePath(), "D:\\develop\\repositoryJar\\" + file.getName());
}
}
}
System.out.println(num);
}
public static List<File> getallfile(String path) {
List<File> allfilelist = new ArrayList<File>();
return getallfile(new File(path), allfilelist);
}
public static List<File> getallfile(File file, List<File> allfilelist) {
if (file.exists()) {
//判断文件是否是文件夹,如果是,开始递归
if (file.isDirectory()) {
File f[] = file.listFiles();
for (File file2 : f) {
getallfile(file2, allfilelist);
}
} else {
allfilelist.add(file);
}
}
return allfilelist;
}
/**
* 字符流方式复制文件,一次复制一个字节
* @param srcFilePath 源文件路径
* @param destFilePath 新文件路径
*/
public static void copyFileReaderToWriterChar(String srcFilePath,String destFilePath){
FileReader fileReader = null;
FileWriter fileWriter = null;
try{
fileReader = new FileReader(srcFilePath);
fileWriter = new FileWriter(destFilePath);
int charRead;
while((charRead = fileReader.read()) != -1){
fileWriter.write(charRead);
}
fileWriter.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(null != fileReader){
try{
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != fileWriter){
try{
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}