2015-09-03 44 views
0

你能告訴我,如何保存java swing應用程序中最後選定的文件夾,以便下次啓動我的應用程序時自動加載?如何在關閉應用程序後保存上次選擇的文件夾?

我正在寫一個應用程序,它讀取圖像並形成幻燈片。我有一切,但記錄選定的文件夾?

任何想法?

+0

退房'Properties'在java中http://docs.oracle.com/javase/ 7 /文檔/ API/JAVA/UTIL/Properties.html。可以將狀態保存到屬性中,並在下次再次讀取它。 –

+0

看看[這個討論](http://stackoverflow.com/questions/19556932/how-to-save-the-state-of-my-minesweeper-game-and-then-load-it/19557052# 19557052)的一些想法。在這種情況下,我傾向於使用'Preferences' API來簡化它 – MadProgrammer

回答

1

任何想法?

有許多可能的方法,但這裏有一對夫婦直截了當的國家:

  • 記錄要在一個Properties對象持久化應用狀態。在應用程序退出時,使用其中一種保存/存儲方法將屬性保存到文件中。當應用程序重新啓動時,從文件加載狀態...如果存在。

  • 閱讀關於Java Preferences的API。

+0

謝謝!有用! – Woytas

1

我覺得你的最簡單的方法是Preferences類,基本的工作原理是這樣的:

//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 
相關問題