When using jfilechooser in java to select a file,We always want to save the last browsing history when we open it next time,When opening the file dialog,Always trace back to the previous path.

There is a very stupid way,That is to save the path of the selected file to a local file every time you open it,When you open the jfilechooser dialog again,Check to see if there is content.If there is content in the file, the dialog box is opened according to the stored path.

If I say that Java can operate the Windows registry without using JNI, do you believe it?Many software menus have options like "setting" or "preferences" to set or modify the configuration of the software.These configuration information can be saved in a configuration file as described above. If it is a Windows platform, it may also be saved in the system registry.Starting with jdk 1.4, java has added a java.util.prefs package under java.util that deals specifically with user and system configuration information. One of the 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,Therefore, on different systems you may see preferences saved as local files, ldap directory entries, database entries, etc.Like under the windows platform, it is saved to the system registry.Not only that,You can also export preferences as xml files or import from xml files.

① systemnodeforpackage () // Get a preferences object according to the specified class object. The registry path of this object starts from "hkey_local_machine \"

② systemroot () // Get the preferences object with the registry path hkey_local_machine \ software \ javasoft \ prefs as the root node

③ usernodeforpackage () // Get a preferences object according to the specified class object. The registry path of this object starts from "hkey_current_user \"

④ userroot () // Get the preferences object with the registry path hkey_current_user \ software \ javasoft \ prefs as the root node

The following code briefly demonstrates the use of the preference class.Code from online

import java.util.prefs.preferences;
public class preferrencetest {
private preferences prefs;
public void setpreference () {
// this will define a node in which the preferences can be stored
prefs=preferences.userroot (). node (this.getclass (). getname ());
string id1="test1";
string id2="test2";
string id3="test3";
// first we will 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 50
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 first value
prefs.remove (id1);
system.out.println (prefs.get (id2, ""));
public static void main (string [] args) {
preferrencetest test=new preferrencetest ();
test.setpreference ();

Here demonstrates how to implement the last path to select the file to save

preferences pref=preferences.userroot (). node (this.getclass (). getname ());
string lastpath=pref.get ("lastpath", "");
   jfilechooser chooser=null;
   if (! lastpath.equals ("")) {
   chooser=new jfilechooser (lastpath);
   chooser=new jfilechooser ();
// myfilefilter is a file filtering class written by myself,Only accept xls format files
    myfilefilter filter=new myfilefilter ("xls", "accepts only xls format files,Ie excel 2003 version file ");
   chooser.setfilefilter (filter);
 int state;// File selector return state
 state=chooser.showopendialog (null);// Show open file dialog
 file file=chooser.getselectedfile ();// Get 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=description;// Set file description text
  public boolean accept (file file)
  {// Override the accept method in filefilter
    if (file.isdirectory ()) // If it is a directory,Return true
      return true;
    string filename=file.getname ();// Get the file name
    if (filename.touppercase () .endswith (ends.touppercase ()))
    // Compare the file suffix and the acceptable suffix to uppercase
      return true;
      return false;
 public string getends () {
 return ends;
 public void setends (string ends) {
 public string getdescription () {
 return description;
 public void setdescription (string description) {
  • Previous Ant User Guide Detailed Getting Started Tutorial
  • Next Apache ant installation and environment variable configuration tutorial under Windows