Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

What is the best way to save user settings in java desktop app securely? For example If I want to save an Ftp account settings what is the best way to do that?

Thanks

like image 307
Feras Odeh Avatar asked Sep 24 '10 06:09

Feras Odeh


People also ask

What is the most appropriate way to store user settings in Android application?

User settings are generally saved locally in Android using SharedPreferences with a key-value pair. You use the String key to save or look up the associated value.


2 Answers

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

like image 117
Giorgos Dimtsas Avatar answered Sep 19 '22 15:09

Giorgos Dimtsas


I usually store in user data directory, with sub directories of application name followed by application version.

public static String getUserDataDirectory() {     return System.getProperty("user.home") + File.separator + ".jstock" + File.separator + getApplicationVersionString() + File.separator; } 

I had been using the following code for 3 years. This method works quite well either in Windows, Linux or Mac.

Please note that, in Windows, never store it in Program Files, as UAC in Windows Vista or newer may give you a lot of trouble.

Remember put a dot in-front of your application name, so that it will become a hidden folder in Linux.

Good thing by using this methology is that, you are not limited your self in storing primitive value only. Instead, you may save the entire object state to the disk by using xstream

For example :

public static boolean toXML(Object object, File file) {     XStream xStream = new XStream();     OutputStream outputStream = null;     Writer writer = null;      try {         outputStream = new FileOutputStream(file);         writer = new OutputStreamWriter(outputStream, Charset.forName("UTF-8"));         xStream.toXML(object, writer);     }     catch (Exception exp) {         log.error(null, exp);         return false;     }     finally {         close(writer);         close(outputStream);     }      return true; }  
like image 39
Cheok Yan Cheng Avatar answered Sep 21 '22 15:09

Cheok Yan Cheng