Using java.util.prefs.Preferences for multiple classes

I would like to integrate java.util.prefs.Preferences into my desktop application.

I need to have access to user settings from several classes, for example:

public class ClassA
{
    Preferences prefs = Preferences.userRoot();
    prefs.put("PhoneNumber", phoneNumber);

    ...
} 

public class ClassB
{
    Preferences prefs = Preferences.userRoot();
    String phoneNumber = prefs.get("PhoneNumber", "555-555-1212");

    ...
}

After reading the API here: http://docs.oracle.com/javase/7/docs/api/java/util/prefs/Preferences.html

it seems that I should use flush () after calling put () to ensure that changes will be reflected in future calls to get (), and I should also use sync () before get (), like this:

public class ClassA
{
    Preferences prefs = Preferences.userRoot();
    prefs.put("PhoneNumber", phoneNumber);
    prefs.flush();

    ...
} 

public class ClassB
{
    Preferences prefs = Preferences.userRoot();
    prefs.sync();
    String phoneNumber = prefs.get("PhoneNumber", "555-555-1212");

    ...
}

And the documentation even seems to encourage the use of flush () at some point:

" , , , , . flush ".

: , , flush(), sync():

" , , , , . - , - , BackingStoreException".

(sync() BackingStoreException)

, , , flush() get().

, , flush() get(), , , .

+3

All Articles