1

I'm loading a prop and saving it

File propfile=new File(getClass().getResource("credentials.properties").toURI());
                prop.load(new FileInputStream(propfile));
            prop.setProperty("a", username);
            prop.setProperty("c", password);
            prop.setProperty("b", pbKey);
            prop.store(new FileOutputStream(propfile), null);

When i normally run this in netbeans its fine, when its bundled into .jar file it throws

Caused by: java.lang.IllegalArgumentException: URI is not hierarchical
    at java.io.File.(Unknown Source)

now when i use

 getClass().getResourceAsStream("credentials.properties");

i can read the file , but i can't save the file unless i use the .toURI() as in -> Storing changes in .properties file that has been read via getClass().getResourceAsStream

so when i use toURI() and when i run it (jar file) it would cry out saying the URI is not hierarchical

and when i use getResourceAsStream , i couldn't save the file

what should i do? the properties file is in same package as class is in.

Community
  • 1
  • 1
cypronmaya
  • 520
  • 1
  • 11
  • 24
  • @ cypronmaya : why does the program need to modify the jar file? I guess you need to revisit the requirement/design. Consider the case the jar is not writable at all. – Jayan Apr 13 '12 at 07:48
  • @Jayan i'm trying to build a desktop application where some parameters are set by user like their username/password etc., kinda, to implement that i've wanted to use properties file becoz its easier. – cypronmaya Apr 13 '12 at 07:52
  • @ cypronmaya : You can have some sort of defaults in properties from the jar. Save the modified content some where else, say in ${user.home}. – Jayan Apr 13 '12 at 07:54
  • 1
    @cypronmaya Jayan is right, have a look at my answer below for a implementation that should work. – Angelo Fuchs Apr 13 '12 at 08:01

2 Answers2

4

If you only need to load the properties, you shouldn't be using File at all - you should use getResourceAsStream.

If you need to save the properties back again, you can't easily have them in a jar file. You'd need to rebuild the jar file each time you save - ick!

If you really need both, you might want to consider having a file which is created the first time you need to save changes: when loading, use the file if it's present, but use the version in the jar file otherwise.

EDIT: If you're building a desktop application and these are basically user preferences, you should look into the Preferences API. Also be very careful if you're storing passwords... avoid doing so if you possibly can.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • would you mind if you could elaborate the last part 'If you really need both.......' – cypronmaya Apr 13 '12 at 07:49
  • @cypronmaya: Which bit of the suggestion do you not understand? Do you definitely need to save changes? Assuming you *don't* want to start rewriting the jar file, do you have somewhere appropriate you could save the new properties file? – Jon Skeet Apr 13 '12 at 07:52
  • the part u said 'you might want to consider having a file which is created the first time you need to save changes' to save changes? i couldn't do that in anyway, yes i need to save changes atleast to a temporary file until program closes etc., – cypronmaya Apr 13 '12 at 07:56
  • @cypronmaya: Why can't you do that? And if you only need the changes until the program closes, why can't you just keep them in memory? Why use a file at all? – Jon Skeet Apr 13 '12 at 07:59
1

Try:

File userFile = new File(System.getProperty("user.home"), "myProgram.properties");
if(userFile.exists()) {
    prop.load(new FileInputStream(userFile));
} else {
    prop.load(getClass().getResourceAsStream("credentials.properties"));
}

prop.setProperty("a", username);
prop.setProperty("c", password);
prop.setProperty("b", pbKey);
prop.store(new FileOutputStream(userFile), null);

(be aware that user.home does not work every time on every machine, but it should work next to every time.)

Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72
  • 1
    *"user.home does not work on all platforms."* News to me. In what way does it 'not work'? On which platforms? – Andrew Thompson Apr 13 '12 at 08:04
  • @AndrewThompson I've seen it not working already. Although 'platform' is the wrong word here (I'll edit it) See this question as example: http://stackoverflow.com/questions/1503284/java-system-getpropertyuser-home-returns – Angelo Fuchs Apr 13 '12 at 08:06