I'm currently trying to save the entries in a JComboBox after the application terminates. Initially, I was using a BufferedWriter/Reader to do load/save the items that go in the JComboBox. This method did not work because the Jar file did not have access to the src/ folder after compilation. This meant that when running the application from the executable the JComboBox would be empty and unable to save new items.
My next approach was to use the resource folder and leverage the ClassLoader to obtain the resource. Luckily, this approach worked for READING when launch from the jar file and it works for reading and writing in the IDE.
My problem is that the behavior of this application is different when running from IDE and from the jar file. Currently, it can read when launched from the jar but it cannot WRITE to the resource file.
Here's some code for writing to the resource file:
File file= new File(getClass().getClassLoader().getResource("addresses.txt").toURI());
FileOutputStream fs = new FileOutputStream(file);
OutputStreamWriter ow = new OutputStreamWriter(fs);
outputWriter = new BufferedWriter(ow);
for(int i = 0; i < items.length; i++) {
outputWriter.write(items[i]);
outputWriter.newLine();
}
outputWriter.flush();
outputWriter.close();
return true;
}