代碼如下:
package com.famous.dark.util;
import java.io.File;
import java.io.FileFilter;
public class MyFileFilter implements FileFilter {
public boolean accept(File file) {
String fileName = file.getName().toLowerCase();
if (fileName.endsWith(".jar") || file.isDirectory()) {
return true;
}
return false;
}
}
代碼如下:
package com.famous.dark.util;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
/**
*
* @author pat 將文件中的jar文件提取出來
*/
public class JarCollectionUtil {
public String collectionJar(File baseFile, String targetFilePath) throws Exception {
File targetDir = new File(targetFilePath);
if (!targetDir.exists()) {
targetDir.mkdir();
}
if (baseFile.exists() && baseFile.isDirectory()) {
File[] files = baseFile.listFiles(new MyFileFilter());
for (File file : files) {
if (file.isDirectory()) {
collectionJar(file, targetFilePath);
} else {
File targetFile = new File(targetFilePath + "\\" + file.getName());
FileUtils.copyFile(file, targetFile);
}
}
return "success";
} else {
return "文件路徑不存在";
}
}
public static void main(String[] args) throws Exception {
JarCollectionUtil collectionUtil = new JarCollectionUtil();
collectionUtil.collectionJar(new File("F:\\JAR\\lucene-4.3.1"), "F:\\JAR\\LUCENE");
}
}