Problem: I’m having trouble trying to figure out how to save an xml file to within a jar. It seemed to me that this would be a reasonable form of persistence for some settings data. The file is of xml settings for an audio mix. There are currently two files: day.xml and night.xml for two different mixes.
@tldr:
If these is no way to save to the internals of a jar or if doing so is a bad plan, no need to read further. But a suggestion for an alternative plan would be helpful. I’m wanting to ship the jar for use as a library, and want the owner of that library to be able to save/load settings that they set up. The Java Properties solution was discarded because of the hierarchical complexities and multiple settings docs that will probably be needed down the road.
I have managed to read and write xml files to a user’s directory, and I can read xml files from the jar. I specify the read from within the jar as follows:
URL url = this.getClass().getResource("res/day.xml");
document = builder.parse(url.toURI().toString());
where builder is a valid DocumentBuilder.
For output, which doesn’t work, I first tried the following:
URL url = this.getClass().getResource("res/day.xml");
writer = factory.createXMLStreamWriter(new FileOutputStream(new File(url.toURI())));
The error message says that the URI is not heirarchical on the above writer assignment.
I also tried this:
URL url = this.getClass().getResource("res/day.xml");
File file = new File(url.getFile());
This gives a FileNotFoundException. The error message:
file:\C:\Users\Owner\Desktop\vangardaudio.jar!\com\adonax\pfaudio\vangard\res\day.xml (The filename, directory name, or volume label syntax is incorrect)
When inspecting the jar file (by changing its suffix to .zip and drilling down) I can verify that the file exists.
Is there a way to save a file in this manner?
Was looking for a tutorial on our site pertaining to saving settings but came up cold. Is there one that I overlooked? Seem like there probably is one somewhere, since it is a pertinent topic to game making.