How to Save Preference User Settings in Java

How do I save preference user settings in Java?

You can use java.util.prefs package. A simple example:

// Retrieve the user preference node for the package com.mycompany
Preferences prefs = Preferences.userNodeForPackage(com.mycompany.MyClass.class);

// Preference key name
final String PREF_NAME = "name_of_preference";

// Set the value of the preference
String newValue = "a string";
prefs.put(PREF_NAME, newValue);

// Get the value of the preference;
// default value is returned if the preference does not exist
String defaultValue = "default string";
String propertyValue = prefs.get(PREF_NAME, defaultValue); // "a string"

There are many more examples at java2s.com.

What is the best way to save user settings in java application?

The Preferences API is a nice way to store user and system preferences. If you want to store passwords, you'll have to encrypt them. Here is a nice article that can get you started.

Encrypted Preferences in Java

Saving User Settings Inside a JAR

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.

How to use and store User Preferences

I have never seen this be a problem for performance even in some of the 100k user range. Portlet preferences should be used for portlet specific preferences, if they are to be shared over more than one portlet, the better place to store them is into a user store like an ldap. If you use ldap portal takes care of the caching for you. You can also use a look aside databas as part of Portal and store user attributes in the db and they come as part of the user object from PUMA.

With portlet preferences I would recommend caching them so you are not having to go the portal layer every time.

At the end though, preferences specific to a portlet, save to portlet preferences, specific to the user, put in a user store like ldap, look aside or something similar like a portlet service that uses its own database.

Best practices for saving settings?

You can use the Preferences API for this if it is user specific configuration that should be stored and maintained.

Applications require preference and configuration data to adapt to the needs of different users and environments. The java.util.prefs package provides a way for applications to store and retrieve user and system preference and configuration data. The data is stored persistently in an implementation-dependent backing store. There are two separate trees of preference nodes, one for user preferences and one for system preferences.

Saving user settings from GUI

You can use java.util.prefs.Preferences to store the configuration in your JDialog. This question - Java Preference Manager - discuss about how to create frontend+backend solution by using Preferences (something like JFace org.eclipse.jface.preference)

Different ways of saving program settings in Java

Apache has a nice commons configuration library for handling configuration:
https://commons.apache.org/proper/commons-configuration/
This would be good if you'll eventually need a lot more configuration or if you'll maintain several applications and want a common way to handle different kinds of configuration files.

But if all you're looking for is to store a couple properties per user somewhere, I would just use the Properties class and store each respective user's data within a file under their user.home directory called your app name.properties. That way one user's data does not collide with another's, and the chances the user would have write privileges in user.home is pretty good. User.home is definitely available in more systems than windows - it's set for linux/unix envirionments, so that would cover Mac OSX as well.

If you don't already have a database, I wouldn't look to introduce one just to store a couple user preferences.



Related Topics



Leave a reply



Submit