Java to operate the Windows Registry

Source: Internet
Author: User

When we write a program, we sometimes need to save some data, such as the user name or something else, but the data is not stored in the same directory of the program. This is too obvious, it's not safe. What should I do? At this time, if you can save the data to the registry, it would be nice to prepare a large file in the program directory for concealment and convenience.
In Java, you can use Java. util. prefs. preferences to operate the Registry. Let's take a look at the example first.

/*
* Test3.java
*
* Created on, 18:41:41
*
* To change this template, choose tools | templates
* And open the template in the editor.
*/

Package test1;

Import java. util. prefs. preferences;

/**
*
* @ Author hadeslee
*/
Public class test3 {

Public static void main (string [] ARGs) throws exception {
Preferences pre = preferences. systemnodeforpackage (test3.class );
Pre. Put ("username", "hadeslee ");
Pre. putint ("Age", 25 );
Pre. Flush ();
Preferences now = preferences. systemnodeforpackage (test3.class );
System. Out. println (now. Get ("username", "EMPTY "));
System. Out. println (now. getint ("Age", 0 ));

}
}

This is the simplest example. The first sentence is to get a node based on the package, and then put some values into the node, which can be equivalent to the values of string, Boolean, Int, long, float, and double, if you want to store the object, you can serialize the object into byte [] And then store it in, and then read the object. This operation is like operating a map, all values exist in this ing.

So where are they stored?

If systemnode... is selected, save it in [HKEY_LOCAL_MACHINE/software/kerberoft/prefs]

If usernode... is selected, it is saved in [HKEY_CURRENT_USER/software/kerberoft/prefs]
Under the subnode

But again, if we want to program cross-platform, this is not very good. I tried it on Linux. Every time the program exits, I set no value, because there is no registry like windows in Linux, I don't know how sun is implemented in Linux. For portability, it is best to use Java. uitl. properties class performs value operations based on a text ing file. However, it can only store values in string objects, such as byte, if you want to store things too complex, simply use the database. :)

Original article: http://www.blogjava.net/hadeslee/archive/2007/09/20/146881.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.