Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuration Files in java

I have created a Swing Application- GUI containing fields like TextFields, Labels, CheckBoxes and ComboBoxes. When the user enters some information, I want the details of the textfields, comboboxes and checkboxes to be saved into a file, and the next time when a user opens this Window, I want the details that have been saved in the file, i.e. those that have been entered by the user the previous time to be loaded into the GUI. Can anyone please help me in doing this? I hope you understand the question, if not I will explain in a more detailed manner.

Thank you so much in advance.

like image 541
rosebrit3 Avatar asked May 11 '26 16:05

rosebrit3


2 Answers

Here is a simple example using the java.util.prefs package:

// 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"

The way preferences are saved is OS-dependent. On Windows, it will use the registry.

Examples of use: http://www.exampledepot.com/egs/java.util.prefs/pkg.html

like image 94
Guillaume Avatar answered May 13 '26 05:05

Guillaume


The simplest way is to use java.util.Properties that is able to store its content in file (properties or xml format).

If you want more you can use XML. You can either generate and parse it yourself (using DOM or SAX) or, better use higher level API (e.g. JaxB).

You can also use configuration package The list can be continued...

like image 37
AlexR Avatar answered May 13 '26 05:05

AlexR