如何在 Java 中保存首选项用户设置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4017137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 08:29:04  来源:igfitidea点击:

How do I save preference user settings in Java?

javaswingjframejbutton

提问by js0823

For example, I have a window with a preference button. I want to make it so that when user press the preference button and checks his/her appropriate options and press ok, it saves the preference, then when user presses run on the main window, it runs accordingly to preference the user changed on the preference window.

例如,我有一个带有首选项按钮的窗口。I want to make it so that when user press the preference button and checks his/her appropriate options and press ok, it saves the preference, then when user presses run on the main window, it runs accordingly to preference the user changed on the preference窗户。

Thank you in advance.

先感谢您。

采纳答案by Peter Knego

You can use java.util.prefspackage. A simple example:

您可以使用java.util.prefs包。一个简单的例子:

// Retrieve the user preference node for the package com.mycompany
Preferences prefs = Preferences.userNodeForPackage(com.mycompany.MyClass.class);

// Preference key name
final String PREF_NAME = "name_of_preference";

// Set the value of the preference
String newValue = "a string";
prefs.put(PREF_NAME, newValue);

// Get the value of the preference;
// default value is returned if the preference does not exist
String defaultValue = "default string";
String propertyValue = prefs.get(PREF_NAME, defaultValue); // "a string"

There are many more examples at java2s.com.

java2s.com 上还有更多示例

回答by casablanca

There is a Java Preferences APIspecifically for this purpose. It lets you store per-user preferences in an easy cross-platform way, while the API itself takes care of where and how to store the data.

有一个专门用于此目的的Java Preferences API。它允许您以简单的跨平台方式存储每个用户的首选项,而 API 本身负责存储数据的位置和方式。

回答by Andrew Thompson

Besides Preferences, there is another alternative available for rich clients launched using Java Web Start. This alternative is the PersistenceService. Here is a small demo. of the PersistenceService.

除了 Preferences 之外,还有另一种可供使用Java Web Start 启动的富客户端的替代方法。这个替代方案是 PersistenceService。这是一个小演示。的 PersistenceService

It is also a service where the programmer does not need to worry about the details of where the information is stored.

它也是一种服务,程序员无需担心信息存储位置的细节。

回答by Rahul

public void saveProperties() {
    try {            
        String USER_NAME = "Some name";
        String DP_ADDRESS = "Some url";
        //create a properties file
        Properties props = new Properties();
        props.setProperty("User name", USER_NAME);
        props.setProperty("Display picture address", DP_ADDRESS);
        File f = new File("YOUR_TARGET_FILE_PATH");
        OutputStream out = new FileOutputStream( f );
        //If you wish to make some comments 
        props.store(out, "User properties");
    }
    catch (Exception e ) {
        e.printStackTrace();
    }
}

You may use java.util.Propertiesto save your preferences

您可以java.util.Properties用来保存您的偏好