With reference to the link: How do I read a resource file from a Java jar file?
I am trying using your code base and trying to read content of sample.csv
which is residing in my project directory src/main/resources
. I am unable to read the content, it says can not read file. Output:
[Can not read file:
sample.csv
]
//This is added within your while loop after this check /* If it is a directory, then skip it. */
I mean when file is detected then next is my below code snippet added to read the file content
if(entry.getName().contains("sample.csv")) {
File f1 = new File("sample.csv");
if(f1.canRead()) {
List<String> lines = Files.readAllLines(f1.toPath());
System.out.println("Lines in file: "+lines.size());
} else {
System.out.println("Can not read file: "+entry.getName());
}
}
Can anyone educate me what I am doing wrong here, how can I make it working?
My requirement is this:
- (My micro-service)
Service.jar
importsParser.jar
library in its pom.xml - (My library) -
Parser.jar
hasFnmaUtils-3.2-fieldMapping.csv
file insrc/main/resources
directory There is a
FnmaUtils
class that loads theFnmaUtils-3.2-fieldMapping.csv
within its constructor, this class is part ofParser.jar
- Here I am trying to read the contentFnmaUtils-3.2-fieldMapping.csv
, this step is keep failing with below error, tried all possible options shown in [How do I read a resource file from a Java jar file?public FnmaUtils() { String mappingFileUrl = null; try { Resource resource = new ClassPathResource("FnmaUtils-3.2-fieldMapping.csv"); mappingFileUrl = resource.getFile().getPath(); loadFnmaTemplate(mappingFileUrl); } catch (Exception e) { e.printStackTrace(); LOGGER.error("Error loading fnma template file ", e); } }
Getting error:
java.io.FileNotFoundException: class path resource [`FnmaUtils-3.2-fieldMapping.csv`] cannot be resolved to absolute file path because it does not reside in the file system: `jar:file:/home/ravibeli/.m2/repository/com/xxx/mismo/util/fnma-parser32/2018.1.0.0-SNAPSHOT/fnma-parser32-2018.1.0.0-SNAPSHOT.jar!/FnmaUtils-3.2-fieldMapping.csv`
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:218)
at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52)
at com.xxx.fnma.util.FannieMaeUtils.<init>(FannieMaeUtils.java:41)
at com.xxx.fnma.processor.FNMA32Processor.<init>(FNMA32Processor.java:54)
at com.xxx.fnma.processor.FNMA32Processor.<clinit>(FNMA32Processor.java:43)
What is going wrong here?