我有一个名为prop.properties的属性文件。在我的主代码中,我同时拥有System.getProperty()和properties.getProperty()。
我的问题是:它们是都从prop.properties获取属性,还是从不同的地方获取属性,properties.getProperty()从prop.properties获取属性,System.getProperty()从其他地方获取属性。
发布于 2013-06-17 23:46:36
System.getProperty()获得一个由JVM定义的属性(要么是JVM本身,要么是您在命令行传递的任何-D选项)。定义的属性列表可以在here上找到(感谢@NikitaBeloglazov)。
properties.getProperty()是某人初始化Properties类型的对象的结果。它们是不同的,尽管you can get what System has as a Properties instance。
Properties对象通常是加载Java属性文件的结果(参见here了解如何完成此操作)
发布于 2013-06-17 23:47:43
System.getProperty(propName)是System.getProperties().getProperty(propName)的快捷方式。
然而,java.util.Properties只是java.utils.Hashtable的一个子类,所以它的实例可以在代码中的任何地方创建,并填充任何数据。显然,代码
Properties props = System.getProperties();
props.getProperty("os.name");等同于
System.getProperty("os.name");然而,
Properties props = new Properties();
props.load(new FileInputStream("myprops.properties"))
props.getProperty("os.name");是不一样的。
发布于 2013-06-17 23:49:18
System类指的是您正在运行的JVM (它将从您的操作系统获取信息)。当您在System上使用getProperty时,您将获得实际的属性。
Property类基本上是一个美化的哈希表。你可以完全自己定义它,所以当你执行getProperty()时,你会得到你设置的结果。XML类的用处在于它有一个内置的Property解析器,因此您可以从文件中读取属性。
https://stackoverflow.com/questions/17151547
复制相似问题