Reputation: 25
Hello I wrote a game in Java using Eclipse and I manage preferences through a class that reads / writes a configuration txt file. When I run it from eclipse everything works fine but when I run it from the jar I created both reading and writing do not work. I managed to fix the reading problem using getResourceAsStream but I do not know how to solve the writing problem. This is the code I use to read the file:
try {
InputStream is = getClass().getResourceAsStream("/res/configuration.txt");
Properties properties = new Properties();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
properties.load(br);
br.close();
}
and properties.getProperty to get values. Works both from Eclipse and jar.
This is the code I tried to write the file:
try {
properties.setProperty("playerColor", value);
FileOutputStream fos = new FileOutputStream("/res/configuration.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos);
properties.store(osw, null);
osw.close();
}
Does not work in any case, it says it can not find the file.
How can I fix that? Also, I have to use the class Properties in the code.
Thanks
MORE INFO
I use two csv file to save records and games and a txt file for configuration, that are located in the package "res" this way:
Project Folder
--- classpath
--- bin
--- src
--- project
--- res
--- records.csv
--- save.csv
--- configuration.txt
Upvotes: 2
Views: 75
Reputation: 486
Well first of all a jar is an archive, which isn't meant to be changed. So the right solution would be to write to an external source. You could create a folder and put all data from your game inside that folder (statistics, save files, etc)
Upvotes: 1