The example of this article tells the method of saving the last record using the Java using the Preference class. Share it for everyone for your reference. The specific analysis is as follows:
When using JFileChooser in Java to select files, we always hope that when we open the record next time, we can always go back to the last path when opening the file dialog box.
There is a very stupid way, that is, to save the path of the selected file into the local file every time you open it, and then open the JFileChooser dialog box, first check whether there is content. Open the dialog box.
If I say that you can use JNI to operate the registry of Windows without using JNI? Many software menus have options such as "setting" or "preferences" are used to set or modify the configuration of the software. These configuration information can be saved in a configuration file like above. If it is Windows platform, it is also under the Windows platform. It may be stored in the system registry. Starting from JDK 1.4, Java has added a Java.util.prefs package specializing in the processing information of user and system configuration under Java.util. One of the preferences is a relatively "advanced" thing.
In essence, Preferences itself is a thing that has nothing to do with the platform, but different OS implementation of its SPI (Service Provider Interface) is related to the platform. Therefore Save as local files, LDAP directory items, database strips, etc., such as under the Windows platform, it is stored in the system registry. Not only that, you can also export the premiere to XML files or import from XML files.
① SystemNodeForPackage () // Obtain a Preferences object based on the specified class object.
② Systemroot () //
③ UsernodeForpackage () // Obtain a Preferences object based on the specified class object.
④ UserRoot () // Get the registry path HKEY_CURRENT_USER/SOFTWARE/JAVASOFT/Prefs as the Preferences object of the nodes
The following code briefly demonstrates the usage of the Preference class. The code comes from the Internet
Import Java.util.Prefrencess; Public Class PREFERNCETEST {Private Preferences Prefs; Public Void Setpreference () {// This Will Define A Node In Whic. h the preferences can be stored prefs = preferences.userroot (). Node (this.getClasss () .getName ()); String id1 = "test1"; string id2 = "test2"; string id3 = "test3"; // first we will get the values // definean value system.prin tln (prefs .getBoolean (id1, true)); // define a string with default "Hello World System.out.println (PREFS.GET (ID2," Hello World "); // Define a Integer with 50 system.out. Println (prefs.getIntint (ID3, 50)); // now set the value of the values prefs.putboolean (ID1, false); prefs.put (id2, "hello europa"); Delete the Preference Settings for the First Value Prefs.remove (ID1); System.out.println (prefs.get (ID2, ""); PreferrenceTest Test = New PreferrenceRenceTest ( ); Test.setpreference ();}}Here, how to achieve the selection file to save the previous path
Preferences pref = preferences.userroot (). Node (this.getclass (). Getname ()); string lastpath = pref.get ("lastpath", ""); if (! LastPath.equals (" ")) {Chooser = New JFilechooser (LastPath);} Else Chooser = New JFileChooser (); // MyFilefilter is a file filter written by myself, only the XLS format file MyFileFilter Filter = NEW M yfilefilter ("xls", " Accept the XLS format file, that is, the Excel 2003 version of the file "); Chooser.SetFilefilter (Filter); int State; // file selector returns state = chooser.showOpendialog (NULL); = chooser .getSelectFile (); // Get the selected file pref.put ("LastPath", file.getpath ()); Import java.io.file; Import Class myfileFilter Extends FileFilter {Public String Ends; // File suffix Public String Description; // File Description Public MyFileFilter (String Ends, String Description) {// Construction function this.ends = ENDS; // Set file suffix This.descripting = description // Set the file description Text} Public Boolean Accept (File File) {// Re -load the access method if (file.isdirectory ()) // If it is a directory, return True Return True; String Filename = f ILE. getName (); // Get the file name if (filename.touppercase () .ndswith (ends.touppercase ())) // After converting the file suffix and acceptable suffixes into return true; IC String GETENDS () {Return Ends;} Public Void Sever (String Ends) {This.ends = ENDS;} Public String GetDescription () {Return Description;} publi. C Void SetDescript (String Description) {This.descripting = Description;}}It is hoped that this article is helpful to everyone's Java program design.