My program has a function that read/write file from resource. This function I have tested smoothly.
For example, I write something to file, restart and loading again, I can read that data again.
But after I export to jar file, I faced problems when write file. Here is my code to write file:
URL resourceUrl = getClass().getResource("/resource/data.sav");
File file = new File(resourceUrl.toURI());
FileOutputStream output = new FileOutputStream(file);
ObjectOutputStream writer = new ObjectOutputStream( output);
When this code run, I has notice error in Command Prompt:

So, My data cannot saved. (I know it because after I restarted app, nothing changed !!!)
Please help me solve this problem.
Thanks 🙂
You simply can’t write files into a jar file this way. The URI you get from
getResource()isn’t afile:///URI, and it can’t be passed tojava.io.File'sconstructor. The only way to write a zip file is by using the classes injava.util.zipthat are designed for this purpose, and those classes are designed to let you write entire jar files, not stream data to a single file inside of one. In a real installation, the user may not even have permission to write to the jar file, anyway.You’re going to need to save your data into a real file on the file system, or possibly, if it’s small enough, by using the preferences API.