Java Preferences API using a custom file? - java

Is it possible to use Preferences API to store my application's settings in a custom file? From what I have read, in Windows preferences are stored in registry which is not a good idea imho and I want my app to save prefs to let's say D:\app.preferences. Is it possible?

the point of the Preferences API is to make it so that the application programmer doesn't have to know anything about how the OS chooses to save user preferences. if you want to save preferences in a specific file, you can certainly do that. just use a Properties instance to read/write to the file you desire.

You can implement your own Preferences, just look at the Linux implementation which internally uses a file. You just wont be able to use the static methods in Preferences to get at the roots but you have to store them somewhere yourself.

By exporting and importing the preferences you can have what you want. It will still also be stored in wherever/whatever OS-specific storage mechanism JAVA chooses

Related

how to allow the user to manually edit a properties file in an android app?

I have an android app that uses a properties file that is currently stored on the sdcard.
I want to allow the user to manually edit this properties file.
Is there a preferred editor that people use or do you roll your own for your app?
Is there a preferred editor that people use
I am not aware of any Android device that ships with a text editor, let alone a dedicated properties file editor. I would be fairly surprised if there are any.
The Android SDK does not ship with a properties file editor, unless you count EditText. I am not aware of any libraries that implement a properties file editor.
do you roll your own for your app?
Most Android developers do not use properties files on-device. Instead, they use preferences, which come with their own UI (PreferenceScreen and kin) and backing store (SharedPreferences, plus PreferenceDataStore on Android O).
You would need to create your own UI for editing your properties. Or, depending upon your use case, you might implement a more traditional preference-based system, using the properties file for import/export.

Saving User Settings Inside a JAR

I've created an application that is going to be run on Windows, Mac OX and Linux. I need to be able to store and read user settings on the fly.
User settings take the form of strings and a key and value pair would work well.
I'm currently using a properties file however this can't be written too on the fly within a JAR.
I'm struggling to find alternatives, what are the options to be able to do this?
You can save the properties file in the user home directory using System.getProperty("user.home") assuming the security manager, if any, allows it. Using properties files to save preferences allows editing the preferences from outside the application.
Another option is to use the Java preferences API for a transparent and platform-independent way of persisting the preferences.

Truelicense Java preferences store other than registry

For managing the license in my Java application, I want to use truelicense.
Truelicense is using Java Preferences to store license keys and Java preferences on windows OS getting stored in Registry under user root or system root.
But my requirement is different. I want to install my application in USB storage and want to allow application access from that USB device only (from any machine). So, I can't use registry to store the license key.
Is there any other way to store Java preferences like file based or any other. And which is the most secure way.
Is truelicense supports storage other than Preferences?
By default Java preferences are getting stored in registry on windows platform.
Alternative of the registry is to story preferences in file.
File based preferences can be used for truelicense. Following is the link showing how to implement file based java preferences.
http://www.davidc.net/programming/java/java-preferences-using-file-backing-store
Hope, this will be useful in your case.

Is writing into class resources a good way to save files?

When we want to load a static file e.g. a picture, a sound file, a file containing information about a game map,... we can store them as resources in jar file and use getClass.getResource("images/splash.png") (also getResourceAsStream) to load and use them. But when we want to read and write into a file like settings file, I don't think using resources is a good way, because i think resources are designed to store read/only files that are not supposed to change, like splash screen image or a game's background music; These are my reasons to think this way:
That is why return value of getResourceAsStream is an instance of InputStream and we don't have a similar function which gives us an OutputStream, because we're not supposed to alter resource files.
Writing into resources changes program .jar file and i guess it's not a good thing at all; Because if we do so: we can't use check-sums to verify file, if we are a limited user and system administrator doesn't give us write permission we can't make changes into main .jar file, user-specific preferences are hard or impossible to implement,...
So, my questions are:
Which parts of my thoughts and assumptions are right or wrong?
If they're right what is the best(I mean short and portable between OSs and Computers) way to store files like that? (Application setting/preferences, A game save file, ...)
(#Some user who may wants to mark this as duplicate: I don't think my question is a duplicate, i searched in the site, I admit it has some common parts with some questions but it's not duplicate!)
Your three observations in #2 above are valid reasons not to store settings in a resource file, regardless of the APIs provided.
There are a variety of ways to save settings in Java, including:
The Java system property "user.home" provides the user's home directory, to which the user should have write access. You can create an application-specific subdirectory underneath it.
Java provides a Preferences API. This may store settings in a directory or (on Windows) in the registry.
OSGI provides a preferences API.
If you're using the Eclipse RCP, you can write to the configuration directory using a ConfigurationScope. See the Eclipse FAQ "What is a preference scope").

Eclipse RCP: Making use of configuration directory

My Eclipse RCP application requires a configuration file that contains some information to connect to a remote database. Where is the best location to store this configuration file?
Can I use the default configuration directory (where 'config.ini' is usually stored) for this purpose? If so, how can I get a File instance to this location programmatically? I also note that this directory does not exist in my Eclipse IDE.
Thanks.
You have, as always, a number of options, depending on your requirements.
use the Runtime Preferences to store in a PreferenceStore with a suitable PreferenceInitializer. Quite a large and extensive API with quite a lot of thought gone into it. The preferences aren't exposed to the user or admin by default, so you'd need to do some work to expose a preference page, or write to a properties file.
For less advanced/less work, especially if you don't have access to the eclipse preferences (e.g. server side OSGi):
set as a system property, in the RCP.ini. Not user-changeable after launch, requires access to the RCP.ini (eclipse.ini) file which may be possible especially if you're not contributing the the IDE.
set as a system property, as an argument in the shortcut. Depends on the user using the shortcut. Specialized shortcut needs to be generated at installation time.
If accessibility from the filesystem is really important, then I would consider using one of the methods above to set an etc directory, and the let your bundles generate default properties files in the etc directory if they don't exist on first use. This is essentially rolling your own preference store, so if you do have access preferences bundle, you may be better off doing that. This rather old User Settings FAQ may also be helpful.
I do recall an Erich Gamma (as in Gang of Four, and JDT technical lead) interview in which he says that there are about seven different preference mechanisms, and he never knew which one to use.
As already pointed out, the Preferences API is something to look at. There is also the Secure Preferences API which is suitable to store user names and passwords encrypted on disc.
Another option is to use the 'org.eclipse.osgi.service.datalocation.Location' OSGi service. This provides access to the different locations available.
A third option is to define a system property in 'config.ini' which points to file with your connection information using placeholders: 'my.connection.settings=#config.dir/mysettings.ini'. '#config.dir' is a placeholder which gets replaced with the actual path to the configuration directory.
Take a look at the resources plugin - might give you what you're looking for:
http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse.platform.doc.isv/guide/resInt_filesystem.htm
Usually, I like to hide the config files in a "bin" directory, or somewhere not in the root directory. You should probably keep it in a sub-directory of your project so you don't clutter up some random location on the system. If you need to get a handle to the File, you can just do:
File configFile = new File("./bin/remoteDbConfig.ini");
Then if its a true ini file, you can use Properties.load() to load and use the values from the ini file.
You could also use the Preferences API to store the data you need for the remote connection.
To get the file location of the Configuration directory, run:
new org.eclipse.core.runtime.preferences.ConfigurationScope().getLocation().toFile();

Categories