views:

395

answers:

5

I am attempting to Store() the change made to my application's Properties. The .Properties file is located in 'resources' package, which is different from the package that contains my UI and model.

I opened the package using:

this.getClass().getClassLoader().getResourceAsStream("resources/settings.properties")

Is there a functional equivalent of this that permits me to persist changes to the Properties Class in the same .Properties file?

+3  A: 

When you wrap your app up as a JAR file, your properties file will be one (possibly compressed) file within that JAR, and it would be a bad idea to try to write to your own JAR.

getResourceAsStream() is meant to open resources for reading, and these can be anywhere on the classpath. You can't write to URLs or inside JARs, you can only write to files, so it doesn't make sense to give you the same API for output.

Find yourself a directory you're allowed to write into, and write your properties there.

It may be a good idea to copy your properties from your installation classpath (possibly inside a JAR) directly out to a file if it doesn't yet exist, as a first operation upon application startup. This will give you a properties file you can write to, yet the master copy of this properties file will come from your project deliverable.

Carl Smotricz
A: 

In addition to Carl's answer, if you're going to read and write to this file frequently, and expect that your application will expand in scope, consider whether to go one step (or several steps) further and use a file-based database like SQLite. There are a few JDBC wrappers for SQLite that would allow you to go beyond the basic string key-value lookup that the Java Properties interface provides.

seh
A: 

in 'resources' package, which is different from the package that contains my UI and model.

You should use the context's classloader for this.

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
properties.load(classLoader.getResourceAsStream("resources/settings.properties"));

Is there a functional equivalent of this that permits me to persist changes to the Properties Class in the same .Properties file?

You need the Properties#store() for this, which can take either an OutputStream or a Writer. First you need to get the URL of the properties file and get its URI so that you can create a File for this which you wrap in a FileOutputStream.

URL url = classLoader.getResource("resources/settings.properties");
properties.store(new FileOutputStream(new File(url.toURI())), null);
BalusC
thanks for your help.
Craig
+2  A: 

It sounds like you want to store user preferences. Consider using the Java Preferences API for that.

Michael Borgwardt
+3  A: 

In general, you cannot put stuff back into a resource you got from the classloader:

  • Class loader resources are often read-only; i.e. held in read-only files / read-only directories.

  • If you got the resource from a JAR file, JAR files are not simply updateable. (To "update" you need to extract the old JAR's contents and create a new JAR with the updated contents. It is to do with the structure of ZIP files ...)

  • In some cases, the class loader resource will have been downloaded on-the-fly, and there is no way to push changes back to the place where you downloaded from.

Even if you can update a resource you got from the classloader, it is a bad idea / bad practice.

  • Doing this "polutes" the clean application installation with a user's preferences. This means that it cannot be shared with other users.

  • There are security issues with having applications installed as writeable so that embedded preferences can be updated. Think viruses. Think nasty users.

  • There are resource management issues with having user-specific copies of applications. And if the user has to install his own copy, there are security issues as well.

Finally, this is NOT the way that system administrators (and educated users) expect software to behave. It is best practice to deal with user preferences in the expected way. For a Java-centric solution use the Java Preferences API. Alternatively, you can read / write Properties files or (yuck) use use a Window specific API to put preferences into the registry.

Stephen C
Big +1. Wise words.
Pascal Thivent
thanks for your thoughts.
Craig