Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java provide some sort of Register\"Cookies" for storing of temporary data?

Does the Java provide some sort of registry- or cookie mechanism where I can store small pieces of data to load next start I start an java application- or applet?

For example the application settings such as last opened file etc

like image 559
Viktor Sehr Avatar asked Dec 14 '22 03:12

Viktor Sehr


2 Answers

See the Java Preferences API.

It allows you to store preferences per-user and per-system. It'll store preferences in hidden files on Unix/Linux, and use the registry in Windows-based systems (although that's implementation-dependent).

Note: I'm not sure that it'll work with applets (due to security restrictions).

like image 105
Brian Agnew Avatar answered Dec 15 '22 17:12

Brian Agnew


Java Properties are widely used both as persistence format (file) both in memory. They are also widely supported by tools like IDE, ant, maven, etc.

The class Properties is very simple to use and it has several useful method for your purpose (store, load and store):

Properties preferences = new Properties();
preferences.put("color", "red");
preferences.put("style", "bold");
preferences.store(new FileOutputStream("prefs.properties"), "preferences");

// reload the properties
Properties preferences = new Properties();
preferences.load(new FileInputStream("prefs.properties"));

A Java .properties file looks like:

# You are reading the ".properties" entry.
! The exclamation mark can also mark text as comments.
website = http://en.wikipedia.org/
language = English
# The backslash below tells the application to continue reading
# the value onto the next line.
message = Welcome to \
          Wikipedia!
# Add spaces to the key
key\ with\ spaces = This is the value that could be looked up with the key "key with spaces".
# Unicode
tab : \u0009  
like image 34
dfa Avatar answered Dec 15 '22 18:12

dfa