将属性文件放在 eclipse 项目中的什么位置

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

where to put the properties file in an eclipse project

javaeclipseproperties-file

提问by sweet dreams

Please tell me where exactly do i put my .properties file in my eclipse project. Do I make a separate folder for it ? I want to put it in such a way that I will be able to distribute my project easily in JAR form.

请告诉我我的 .properties 文件在我的 eclipse 项目中的确切位置。我要为它制作一个单独的文件夹吗?我想把它放在这样一种方式,我将能够以 JAR 形式轻松分发我的项目。

Thanks.

谢谢。

EDIT:

编辑:

I want to put the properties file in such a way that it can be easily edited later, on any OS.

我想将属性文件放在以后可以在任何操作系统上轻松编辑的方式。

回答by Francisco Spaeth

The approach I use in order to be able to easily change configuration without redeployment.

我使用的方法是为了能够轻松更改配置而无需重新部署

One version of the property file (with the defaults) in the root of your project, I always let it in the application package (foo.bar.myapp). I load the default one with getResourceAsStream("/foo/bar/myapp/config.properties")or like in the sample relative to the class package.

项目根目录中的一个版本的属性文件(带有默认值),我总是将它放在应用程序包 (foo.bar.myapp) 中。我getResourceAsStream("/foo/bar/myapp/config.properties")在示例中相对于类包加载了默认的 with或 like 。

And additionally I ready a system property:

另外我准备了一个系统属性:

String configFileLocation = System.getProperty("config");

And just override the default with the properties read from the config file passed as property.

只需使用从作为属性传递的配置文件中读取的属性覆盖默认值。

For instance:

例如:

Properties props = new Properties();
props.load(Main.class.getResourceAsStream("mydefault.properties"));

System.out.println("default loaded: " + props);

String configFile = System.getProperty("config");
if (configFile != null) {
    props.load(new FileInputStream(configFile));
    System.out.println("custom config loaded from " + configFile);
}

System.out.println("custom override: " + props);

This would load first your resource stored under your project in foo.barpackage named mydefault.properties, and after it if system property configis configured it will load override the loaded properties with the one the the referred path.

这将首先加载您的项目下存储在foo.bar名为 的包中的资源mydefault.properties,然后如果config配置了系统属性,它将加载覆盖加载的属性与引用路径的属性。

The system property can be set using -Dparameter, in this case would be something like: java -Dconfig=/home/user/custom.properties foo.bar.Main. Or if you are in a web application (Tomcat for instance) you can set this property using CATALINA_OPTS.

该系统属性可以使用设置-D参数,在这种情况下会是这样的:java -Dconfig=/home/user/custom.properties foo.bar.Main。或者,如果您在 Web 应用程序(例如 Tomcat)中,您可以使用CATALINA_OPTS.

回答by Priyank Doshi

src/main/resourcewould be an ideal choice.

src/main/resource将是一个理想的选择。