java 从外部目录加载属性文件

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

Load the properties file from external directory

javaproperties

提问by AKIWEB

Currently I am loading the property files from the Resource Stream like this-

目前我正在像这样从资源流加载属性文件 -

prop.load(LoadTest.class.getClassLoader().getResourceAsStream("database.properties"));

But what I am looking for is if there is any way I can load the property files from any external directory. Suppose my database.propertiesfile is in-

但我正在寻找的是是否有任何方法可以从任何外部目录加载属性文件。假设我的database.properties文件在-

C:\logging-test\database.properties

C:\logging-test\database.properties

Then how can I load the above property file from that location? Any thoughts?

那么如何从该位置加载上述属性文件?有什么想法吗?

回答by Roberto Linares

As @madth3 stated, you can use the other overload of the load() method in the Properties object to pass an InputStream referencing to a file in the filesystem:

正如@madth3 所说,您可以在 Properties 对象中使用 load() 方法的另一个重载来传递一个 InputStream 引用文件系统中的文件:

prop.load(new FileInputStream("C:\logging-test\database.properties"));