Java 相当于 app.config?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/212539/
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-11 11:26:40  来源:igfitidea点击:

Java equivalent to app.config?

javaconfiguration-files

提问by Omar Kooheji

Is there a Java equivalent to .NET's App.Config?

是否有相当于 .NET 的 App.Config 的 Java?

If not is there a standard way to keep you application settings, so that they can be changed after an app has been distributed?

如果没有,是否有一种标准方法可以保留您的应用程序设置,以便在分发应用程序后可以更改它们?

采纳答案by Powerlord

For WebApps, web.xml can be used to store application settings.

对于 WebApps,web.xml 可用于存储应用程序设置。

Other than that, you can use the Propertiesclass to read and write properties files.

除此之外,您可以使用Properties类来读取和写入属性文件。

You may also want to look at the Preferencesclass, which is used to read and write system and user preferences. It's an abstract class, but you can get appropriate objects using the userNodeForPackage(ClassName.class)and systemNodeForPackage(ClassName.class).

您可能还想查看Preferences类,该类用于读取和写入系统和用户首选项。它是一个抽象类,但您可以使用userNodeForPackage(ClassName.class)and获取适当的对象systemNodeForPackage(ClassName.class)

回答by JeeBee

The simple way is to simply have a properties file, e.g., myapp.properties, with all your settings in. It's not a very advanced way to do settings but it suffices, or you can have your own XML based setup, or get them from a database, etc.

简单的方法是简单地拥有一个属性文件,例如 myapp.properties,其中包含所有设置。这不是一种非常高级的设置方法,但它就足够了,或者您可以拥有自己的基于 XML 的设置,或者从数据库等

回答by Pursuit

To put @Powerlord's suggestion (+1) of using the Propertiesclass into example code:

将@Powerlord 使用Properties该类的建议 (+1)放入示例代码中:

public class SomeClass {
    public static void main(String[] args){
        String dbUrl = "";
        String dbLogin = "";
        String dbPassword = "";     
        if (args.length<3) {
            //If no inputs passed in, look for a configuration file
            URL configFile = SomeClass.class.getClass().getResource("/Configuration.cnf");
            try {
                InputStream configFileStream = configFile.openStream();
                Properties p = new Properties();
                p.load(configFileStream);
                configFileStream.close();

                dbUrl      = (String)p.get("dbUrl");
                dbLogin    = (String)p.get("dbUser");
                dbPassword = (String)p.get("dbPassword");               
            } catch (Exception e) {  //IO or NullPointer exceptions possible in block above
                System.out.println("Useful message");
                System.exit(1);
            }
        } else {
            //Read required inputs from "args"
            dbUrl      = args[0];
            dbLogin    = args[1];
            dbPassword = args[2];           
        }
        //Input checking one three items here
        //Real work here.
    }
}

Then, at the root of the container (e.g. top of a jar file) place a file Configuration.cnfwith the following content:

然后,在容器的根目录(例如 jar 文件的顶部)放置一个Configuration.cnf包含以下内容的文件:

#Comments describing the file
#more comments
dbUser=username
dbPassword=password
dbUrl=jdbc\:mysql\://servername/databasename

This feel not perfect (I'd be interested to hear improvements) but good enough for my current needs.

这感觉并不完美(我有兴趣听到改进)但足以满足我当前的需求。