简体   繁体   English

如何为属性文件指定路径

[英]How to Specify Path for Properties file

I am using Config. 我正在使用Config。 properties file for passing parameters to my methods Now i am loading file from 用于将参数传递给我的方法的属性文件现在我正在从

Properties Config= new Properties(); Config.load(new FileInputStream("C:\\\\Config. properties "));

As i don't want to keep it hard coded how can i set it with package level. 由于我不想对其进行硬编码,因此如何设置软件包级别。 or within application. 或在应用程序内。

Thanks in Advance. 提前致谢。

Make use of ResourceBundle Class. 利用ResourceBundle类。 You just need to specify the properties file name. 您只需要指定属性文件名。 It will take the file from any path,provided the path should be in the classpath. 只要路径应在类路径中,它将从任何路径获取文件。

Example: 例:

// abc.properties is the properties file,which is placed in the class path.You just need to 
// specify its name and the properties file gets loaded.
ResourceBundle s=ResourceBundle.getBundle("abc");
        s.getString("key");   //any key from properties file...

我也只是建议,但您也可以通过命令行参数将完整路径传递到配置文件,例如:

java YourApp -config C:\\config.properties

A properties file packaged with the application should not be loaded using the file system, but using the class loader. 与应用程序打包在一起的属性文件不应使用文件系统来加载,而应使用类加载器来加载。 Indeed, the properties file, once the application is packaged, will be embedded inside a jar file, with the .class files. 确实,将应用程序打包后,属性文件将与.class文件一起嵌入jar文件中。

If the config.properties file is in the package com.foo.bar , then you should load it using 如果config.properties文件位于com.foo.bar包中,则应使用

InputStream in = SomeClass.class.getResourceAsStream("/com/foo/bar/config.properties");

Or with 或搭配

InputStream in = SomeClass.class.getClassLoader().getResourceAsStream("com/foo/bar/config.properties");

You may also load it with a relative path. 您也可以使用相对路径加载它。 If SomeClass is also in the package com.foo.bar , then you may load it with. 如果SomeClass也位于com.foo.bar包中,则可以加载它。

InputStream in = SomeClass.class.getResourceAsStream("config.properties");

Note that Java variables should always start with a lowercase letter: config and not Config . 请注意,Java变量应始终以小写字母开头: config而不是Config

将配置文件放在类路径(您的.class文件所在的位置)中,并使用

getClass().getClassLoader().getResourceAsStream(_path_to_config_file);

There are two ways to get the path of the config files at runtime. 有两种方法可以在运行时获取配置文件的路径。

a) Getting it from database. a)从数据库中获取。 b) Getting it from custom properties of JVM configured at server level Best process is "b" , you can change the properties of JVM at any time if path is changed and just restart the server. b)从在服务器级别配置的JVM的自定义属性中获取它最佳过程是“ b”,如果更改了路径,则可以随时更改JVM的属性,然后只需重新启动服务器即可。

If it's just the path you're worried about then you can use a relative path: 如果这只是您担心的路径,则可以使用相对路径:

Config.load(new FileInputStream("Config.properties"));

This will look in the current working directory. 这将在当前工作目录中查找。 The upsdie: dead simple. 坏话:简单死了。 The downside: it's not that robust. 不利之处:它没有那么强大。 If you start your application from somewhere else without changing the working directory before, the file won't be found. 如果您从其他地方启动应用程序而之前未更改工作目录,则将找不到该文件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM