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
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.
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.
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
)
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.
Best way to save personal user settings who are used on java desktop application app and website
You have to save these personal user settings somewhere and you will always want to use some sort of datastore, like: SQL databases
, NOSQL
, flat files.
I think that SQL databases are usually way to go, and having more then 100 personal options shouldn't degrade performance too much.
But my advice will be to avoid having multiple tables. You can achieve desired effect, with all properties stored for all users, by having just one table
, i.e.:
USER_ID | PROPERTY | PROPERTY_VALUE
1 | COLOR | RED
2 | COLOR | BLUE
1 | FONT_SIZE| 12
2 | FONT_SIZE| 14
etc
Storing program settings in Java
I would suggest you use a properties file. It is better to read and it is easier for the user to configure the properties. Look at this short tutorial for an explanation of the .properties
file. To read and write values from and to the file follow this short tutorial.
Have you tried using:
path = "config.txt"
If you use this syntax you should get the file in the same directory as the jar. Keep in mind that you can use the shortcuts ".." and "." to go to a directory.
With ..
you go to the directory above.
With .
you get your actual directory.
Related Topics
Why Does Java Code with an Inner Class Generates a Third Someclass$1.Class File
Java.Rmi.Connectexception: Connection Refused to Host: 127.0.1.1;
Is Executorservice (Specifically Threadpoolexecutor) Thread Safe
Java: Infinite Loop Using Scanner In.Hasnextint()
How to Iterate Over a Priorityqueue
Blank PDF Even with the Simplest Jasperreport Jrxml
Blank PDF Even with the Simplest Jasperreport Jrxml
How to Stop Parsing Xml Document with Sax at Any Time
Why Are Wait() and Notify() Declared in Java's Object Class
JPA Query.Getresultlist() - Use in a Generic Way
Exception Noclassdeffounderror for Cacheprovider
Why in Java Enum Is Declared as Enum<E Extends Enum<E>>