简体   繁体   中英

How to save the last selected folder after closing application?

Could you tell me, how to save the last selected folder in java swing app, so that the next time, when I activate my app it automatically loaded?

I am writing an application that reads images and forming slideshow. I have everything but that record the selected folder?

Any ideas?

Any ideas?

There are many possible approaches, but here are a couple of straight-forward ones:

  • Record the application state that you want to persist in a Properties object. As the application exits, use one of the save / store methods to save the properties to a file. When the application is restarted, load the state from the file ... if it exists.

  • Read up on the Java Preferences APIs.

I think the easiest way for you is the Preferences class, and basically works this way:

//You can get the stored path and for the case there isn´t any you set the default path
Preferences lastdir = Preferences.systemNodeForPackage(Main.class);
String dirStr = lastdir.get("lastdir","c:/default");

//Then you store the new value each time you change the path
lastdir.put("lastdir","c:/new dir");//Stores the new value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM