简体   繁体   English

将属性文件放在Eclipse项目中的位置

[英]where to put the properties file in an eclipse project

Please tell me where exactly do i put my .properties file in my eclipse project. 请告诉我将.properties文件放在Eclipse项目中的确切位置。 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. 我希望以一种可以轻松以JAR形式分发我的项目的方式来放置它。

Thanks. 谢谢。

EDIT: 编辑:

I want to put the properties file in such a way that it can be easily edited later, on any OS. 我希望将属性文件放置在以后可以在任何OS上轻松编辑的方式。

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). 在项目根目录中的属性文件的一个版本(具有默认值),我总是将其放在应用程序包(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. 我使用getResourceAsStream("/foo/bar/myapp/config.properties")加载默认的,或者在相对于类包的示例中加载默认的。

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.bar package named mydefault.properties , and after it if system property config is configured it will load override the loaded properties with the one the the referred path. 这将首先加载存储在名为mydefault.properties foo.bar程序包中的项目下的资源,在此之后,如果配置了系统属性config ,它将使用引用路径替换加载的属性。

The system property can be set using -D parameter, in this case would be something like: java -Dconfig=/home/user/custom.properties foo.bar.Main . 可以使用-D参数设置系统属性,在这种情况下,将类似于: 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 . 或者,如果您在Web应用程序(例如Tomcat)中,则可以使用CATALINA_OPTS设置此属性。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将.properties文件放在Eclipse项目中的位置? - where to put .properties files in an Eclipse project? 将 Log4j.properties 放在 Eclipse 项目中的正确位置在哪里? - Where is the correct location to put Log4j.properties in an Eclipse project? 在哪里放置属性文件? - Where to put a properties file? Eclipse Java 项目的 build.properties 文件位于何处? - Where is the build.properties file located for an Eclipse Java Project? 在哪里将 log4j.properties 放入 Eclipse 中的 Maven 项目中,其中“src”文件夹中有一些“默认包”? - Where to put log4j.properties into Maven project in Eclipse having some "default package" inside "src"-folder? 将src图像放在Eclipse Web项目中的位置 - where to put src images in Eclipse web project 在eclipse中放置属性文件的位置以及如何在本地和服务器上访问路径 - Where to put properties file in eclipse and how to access the path both locally and on server 我应该在web应用程序(maven项目)中将java.util.logging的logging.properties文件放在哪里? - Where should I put logging.properties file for java.util.logging in web application (maven project)? 放置属性文件的位置(与Maven程序集有关) - Where to put properties file (in connection with maven assembly) 哪里把txt文件放在eclipse中,android? - Where to put txt file in eclipse, android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM