Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为属性文件指定路径_Java - Fatal编程技术网

Java 如何为属性文件指定路径

Java 如何为属性文件指定路径,java,Java,我正在使用配置。属性文件,用于将参数传递给我的方法现在我正在从 Properties Config=newproperties(); 加载(新文件输入流(“C:\\Config.properties”) 因为我不想把它硬编码,我怎样才能用包级别设置它。或在应用程序内 提前感谢。将配置文件放在类路径中(您的.class文件所在的位置),然后使用 getClass().getClassLoader().getResourceAsStream(_path_to_config_file); 将配置文件

我正在使用配置。属性文件,用于将参数传递给我的方法现在我正在从

Properties Config=newproperties();
加载(新文件输入流(“C:\\Config.properties”)

因为我不想把它硬编码,我怎样才能用包级别设置它。或在应用程序内


提前感谢。

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

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

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

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

有两种方法可以在运行时获取配置文件的路径

a) 从数据库中获取它。 b) 从服务器级配置的JVM的自定义属性获取
最好的过程是“b”,如果路径发生更改,您可以随时更改JVM的属性,然后重新启动服务器。

有两种方法可以在运行时获取配置文件的路径

a) 从数据库中获取它。 b) 从服务器级配置的JVM的自定义属性获取
最好的过程是“b”,如果路径发生更改,您可以随时更改JVM的属性,然后重新启动服务器。

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

java YourApp -config C:\\config.properties

我还想建议,但您也可以通过命令行参数传入配置文件的完整路径,例如:

java YourApp -config C:\\config.properties

与应用程序打包的属性文件不应使用文件系统加载,而应使用类加载器加载。事实上,应用程序打包后,属性文件将与.class文件一起嵌入到jar文件中

如果
config.properties
文件位于包
com.foo.bar
中,则应使用

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

InputStream in = SomeClass.class.getClassLoader().getResourceAsStream("com/foo/bar/config.properties");
您还可以使用相对路径加载它。如果
SomeClass
也在包
com.foo.bar
中,则可以使用加载

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

请注意,Java变量应始终以小写字母开头:
config
而不是
config

与应用程序打包的属性文件不应使用文件系统加载,而应使用类加载器加载。事实上,应用程序打包后,属性文件将与.class文件一起嵌入到jar文件中

如果
config.properties
文件位于包
com.foo.bar
中,则应使用

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

InputStream in = SomeClass.class.getClassLoader().getResourceAsStream("com/foo/bar/config.properties");
您还可以使用相对路径加载它。如果
SomeClass
也在包
com.foo.bar
中,则可以使用加载

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

请注意,Java变量应始终以小写字母开头:
config
,而不是
config

使用ResourceBundle类。您只需要指定属性文件名。它将从任何路径获取文件,前提是该路径应位于类路径中

例如:

// 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...

利用ResourceBundle类。您只需要指定属性文件名。它将从任何路径获取文件,前提是该路径应位于类路径中

例如:

// 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...

如果只是您担心的路径,那么您可以使用相对路径:

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

这将在当前工作目录中查找。结果:非常简单。缺点:它没有那么强大。如果您以前从其他地方启动应用程序而不更改工作目录,则将找不到该文件。

如果只是您担心的路径,则可以使用相对路径:

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

这将在当前工作目录中查找。结果:非常简单。缺点:它没有那么强大。如果以前从其他地方启动应用程序而不更改工作目录,则将找不到该文件。

是否将“作为”字段放入公共枚举?您要么需要将路径存储在一些.java源文件中,要么存储在一些属性文件中,而后一种“解决方案”显然是愚蠢的?您或者需要将路径存储在一些.java源文件中,或者存储在一些属性文件中,后者的“解决方案”简直是愚蠢透顶。非常感谢!非常有帮助。非常感谢!非常有用。