I want to do an import that looks inside a folder and cycles through the files found there and then imports stuff.
In my project I have the folder under src/main/resources but in the actual jar it will be packed as a folder on the root (not inside a resources folder) so I don't know how to get the folder path giving only it's name.
I have tried via getResources and passed the folder name as string and also tried something like File folderPath = new File("./import")
(to use the relative path) but that didn't work either.
The difference between the suggested post and mine is that in there that folder path is known(it is a fixed hardcoded location) I need to find that based on class loading or something similar
Below it's the code I am using
public static final String IMPORT_FILES_LOCATION ="import"; //folder name
Enumeration<URL> folderURL = null;
try {
folderURL = getClass().getClassLoader().getResources(IMPORT_FILES_LOCATION);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (folderURL != null && folderURL.hasMoreElements()) {
File folderFile = new File(folderURL.nextElement().getPath());
File[] filesToImport = folderFile.listFiles();
if(filesToImport.length > 0) {
for (File fileToImport : filesToImport) {
System.out.println("import stuff");
}
}
}
Any ideas/suggestions ?
Thanks