I have a Java application that I build with Maven. I have a resources folder com/pkg/resources/...
that I need to access files from. For example directory.txt
. I've been looking at various tutorials and other SO answers, but none seem to work for me. Right now I have:
ClassLoader classLoader = getClass.getClassLoader();
File file = new File(classLoader.getResource("/directory.txt");
When I output f.length
I get a result of 0 bytes. I've also tried:
InputStream is = (this.getClass().getClassLoader().getResourceAsStream("/directory.txt"));
try{
File file = File.createTempFile("dir", ".txt");
Files.copy(is, file.toPath());
} catch (IOException e) {
e.printStackTrace();
}
But that code does not work either. I haven't worked with accessing files from a JAR before. Any ideas on what might be going wrong?