When I put value in a Java Preference does it automatically flush? -


i using java.util.prefs.preferences store application settings. here sample code-

private static final preferences mpreferences         = preferences.userroot().node("lab_exam");  public static void setdefaultpath(java.nio.file.path v) {     mpreferences.put(default_path, v.tostring());     flush(); }  public static void flush() {     try {         mpreferences.flush();     }     catch (backingstoreexception ex) {         logger.getlogger(appsettings.class.getname()).log(level.severe, null, ex);     } } 

i flushing mpreference everytime put new value ensure data saved sucessfully. question is, preference automatically flush everytime put something, or doing correct thing?

from javadoc of preferences:

all of methods modify preferences data permitted operate asynchronously; may return immediately, , changes propagate persistent backing store implementation-dependent delay. flush method may used synchronously force updates backing store. normal termination of java virtual machine not result in loss of pending updates -- explicit flush invocation not required upon termination ensure pending updates made persistent.

so, use flush() if want guard against loss in case of abnormal termination of vm, otherwise it's not needed. aware flushing after every change may reduce performance, in general wouldn't expecting update preferences enough issue.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -