How to visit all the jar files in a file tree using Walk File Tree?
I am trying to get all the xml file names which are local and also in the third party jar. I got all which are present locally but not able to get that are present in jar. Please help.
I have written the code in which i am getting all the xml file name from a csv file , and comparing these xml files with the files present in the system.
package com.jmr.profitto.bms;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.*;
import java.nio.file.attribute.*;
import static java.nio.file.FileVisitResult.*;
import static java.nio.file.FileVisitOption.*;
import java.util.*;
import javax.annotation.Resource;
import org.hibernate.validator.internal.util.privilegedactions.GetClassLoader;
import com.jmr.profitto.common.util.L2lSystemProperties;
import com.jmr.profitto.common.util.L2lUtil;
public class FindFiles {
public static class Finder
extends SimpleFileVisitor<Path> {
private final PathMatcher matcher;
public List<String> fileName = new ArrayList<String>();
public Map<String, String> pathName = new HashMap<String, String>();
public List<String> sectionKeyList;
Finder(String pattern) {
matcher = FileSystems.getDefault()
.getPathMatcher("glob:" + pattern);
}
// Compares the glob pattern against
// the file or directory name.
// If the file is present, put it in the form of key value pair
void find(Path file) {
Path name = file.getFileName();
if (name != null && matcher.matches(name)) {
fileName.add(name.toString());
pathName.put(name.toString(), file.toFile().getParentFile().getName());
}
}
//check whether the map contains the entered file or not
boolean containFile(String sectionKey){
Set<String> keySet = pathName.keySet();
return keySet.contains(sectionKey);
}
//Fetch parent folder of a file.
String getParent(String sectionKey){
return pathName.get(sectionKey);
}
// Invoke the pattern matching
// method on each file.
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) {
find(file);
return CONTINUE;
}
// Invoke the pattern matching
// method on each directory.
@Override
public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs) {
find(dir);
return CONTINUE;
}
//To check the entered file name in the map
void showResult(Finder finder, String sectionKey){
if(!finder.containFile(sectionKey)){
System.out.println(sectionKey);
}
}
//Read the file created from the db to read the section keys present in it
void readSectionKeyForDiffSchema(String fileLocation) throws FileNotFoundException{
BufferedReader bf = new BufferedReader(new FileReader(fileLocation));
sectionKeyList = new ArrayList<>();
String line;
try {
while((line = bf.readLine()) != null){
// use comma as separator
String[] cols = line.split(",");
if(cols.length > 5){
sectionKeyList.add(cols[5]);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args)
throws IOException {
//To create the starting directory of traversal
Path startingDir = Paths.get(".");
//Path startingDir = Paths.get("\\UIData\\");
Scanner sc = new Scanner(System.in);
String fileLocation = sc.next();
String pattern = "-UIData.xml";
//Creates the pattern of finding files
Finder finder = new Finder(pattern);
//Path startingDir = finder.getFilePath();
//To initiate the file walk in file tree
Files.walkFileTree(startingDir, finder);
//To read the csv file created for reading the section keys
finder.readSectionKeyForDiffSchema(fileLocation);
// To show whether the entered file name is present or not
for(int i=1 ;i< finder.sectionKeyList.size() ;i++){
finder.showResult(finder, finder.sectionKeyList.get(i));
}
}
}
Please find the code here.