Java - Saving file from JAR to disk

212 views Asked by At

I'm trying to save a YAML formatted file packaged in the JAR to the user's disk.

The file is named config.yml and sits directly under one of the project's source folders. I can confirm that the file is packaged in the JAR with WinRAR.

I am using the following code to get the file from the JAR:

File file = new File(this.getClassLoader().getResource("config.yml").getPath());

And I am using this code to copy the file to a directory:

FileUtils.copyFileToDirectory(file, this.getDataFolder());

The getDataFolder() method is implemented by a reliable 3rd party API. However, when I use a file instance defined with the same getDataFolder() File instance and the path "config.yml":

new File(this.getDataFolder(), "config.yml")

The console logs a FileNotFoundException. The file path given in the stack trace is this: "file:\C:\Users\Evan\Desk top\Test%20Server\plugins\lottocrates-1.0.jar!\config.yml" which seems correct. I tried opening the file with run prompt, which I was able to open the JAR with, but not the config.yml file.

0

There are 0 answers