Java uses the preference class to save methods for the last record _java

Source: Internet
Author: User
Tags id3

The example in this article describes how Java uses the preference class to save the last record. Share to everyone for your reference. The specific analysis is as follows:

When using Java JFileChooser to select files, we always want to be able to save the last recorded record when we open the next time, that is, when we open the File dialog box, we can always trace back to the previous path.

There is a very stupid way, that is, every time you open the path of the selected files to the local file, and then open the JFileChooser dialog box, first to see if there is content, if the contents of the file in accordance with the stored path to open the dialog box.

If I say Java can operate the Windows registry without using JNI, do you believe it? Many software menus have "Setting" or "Preferences" options for setting or modifying software configurations that can be saved to a configuration file like the one described above, and may be saved to the system registry if it is a Windows platform. Starting with JDK 1.4, Java has added a java.util.prefs package that deals specifically with user and system configuration information under Java.util, and one class preferences is a more "advanced" gadget.

Essentially, preferences itself is a platform-independent thing, but different OS implementations of its SPI (Service Provider Interface) are platform-dependent, so in different systems you may see preferences saved as local files, LDAP directory entries, database entries, and so on, like the Windows platform, it is saved to the system registry. Not only that, you can also export preferences to an XML file or import from an XML file.

①systemnodeforpackage ()///Gets a Preferences object based on the specified class object, and the registry path for this object starts with "Hkey_local_machine\"

②systemroot ()//Get Preferences object with registry path Hkey_local_machine\software\javasoft \prefs as root node

③usernodeforpackage ()///Gets a Preferences object based on the specified class object, and the registry path for this object starts with "Hkey_current_user\"

④userroot ()//Get Preferences object with registry path Hkey_current_user\software\javasoft \prefs as root node

The following code briefly demonstrates the use of the preference class, which comes from the Internet

 import java.util.prefs.Preferences; public class Preferrencetest {private Preferences prefs; public void Setpreference () {//This'll define a node in which the preferences can is stored prefs = Preferences.user 
Root (). Node (This.getclass (). GetName ()); 
String ID1 = "Test1"; 
String ID2 = "Test2";
String ID3 = "Test3"; 
We'll get the values//Define A Boolean value System.out.println (Prefs.getboolean (ID1, true)); 
Define a string with default ' Hello World System.out.println (Prefs.get (ID2, ' Hello World ');
Define a integer with default System.out.println (Prefs.getint (ID3, 50)); 
Now set the values Prefs.putboolean (ID1, false); 
Prefs.put (ID2, "Hello Europa");
Prefs.putint (ID3, 45); 
Delete The preference settings for the ' The ' the ' the ' the ' I Value prefs.remove (ID1); 
System.out.println (Prefs.get (ID2, "")); 
public static void Main (string[] args) {preferrencetest test = new Preferrencetest (); 
Test.setpreference (); } 
}

Here's a demonstration of how to implement a select file to save the previous path

Preferences pref = Preferences.userroot (). Node (This.getclass (). GetName ()); 
   String Lastpath = Pref.get ("Lastpath", ""); 
   JFileChooser chooser = null; 
   if (!lastpath.equals ("")) {chooser = new JFileChooser (Lastpath);
else chooser=new JFileChooser ();
   Myfilefilter is a file filtering class of its own, only accept XLS format file myfilefilter filter = new Myfilefilter ("xls", "only accept XLS format file, that is, Excel version 2003 file");
 Chooser.setfilefilter (filter); int state; The file selector returns the status State=chooser.showopendialog (null);//Displays the Open file dialog box File = Chooser.getselectedfile ();
Get the selected file Pref.put ("Lastpath", File.getpath ());
Import Java.io.File;
Import Javax.swing.filechooser.FileFilter;
  File filter public class Myfilefilter extends FileFilter {public string ends;//file suffix public string description;//File Description text Public Myfilefilter (string ends, string description) {//constructor this.ends = ends;//set file suffix THIS.DESCRIPTION=DESCR Iption; Set file Description text} public boolean accept (file file) {//Accept method in overloaded FileFilter if (file.isdireCtory ())//If it is a directory, returns true return true; String fileName = File.getname ();
    Gets the file name if (Filename.touppercase (). EndsWith (Ends.touppercase ()))////////() compare return True after converting the file suffix and the acceptable suffix to uppercase;
  else return false;
 Public String Getends () {return ends;
 } public void Setends (String ends) {this.ends = ends;
 Public String GetDescription () {return description;
 } public void SetDescription (String description) {this.description = description;

 }
}

I hope this article will help you with your Java programming.

Related Article

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.