简体   繁体   English

如何在属性文件Groovy中设置空对象?

[英]How to set null object in properties file groovy?

I am trying to set a object value to null in properties file but it is always being returned as string. 我试图在属性文件中将对象值设置为null,但始终以字符串形式返回。 Here is the sample code along with the properties file. 这是示例代码以及属性文件。

File propertiesFile = new File('/opt/config.properties')
propertiesFile.withInputStream {
    properties.load(it)
}

    **config.properties**
    spotConfig = null

println properties.spotConfig

But when I am trying to print the above value, it is always returning string whereas I want it to print a null object. 但是,当我尝试打印上述值时,它总是返回字符串,而我希望它打印一个空对象。 How can I do that in groovy? 我如何才能做到这一点? Any help appreciated! 任何帮助表示赞赏!

There is no concept of explicitly assigning null in a properties file. 没有在属性文件中显式分配null概念。 The closest you can get is an empty string as you can read here . 您可以在此处获得的最接近的字符串是一个空字符串。

spotConfig

Or you can simply not specify the key at all. 或者,您根本无法完全指定密钥。

Properties keys and values are strings. Properties键和值是字符串。 So you can't get a null as value from a Properties instance containing that key. 因此,您不能从包含该键的Properties实例中获得null作为值。

In both of these cases, the value will be returned as a string: 在这两种情况下,该值都将作为字符串返回:

x=
y=null

properties.get("x") will return "" , and properties.get("y") will return "null" (the literal string). properties.get("x")将返回""properties.get("y")将返回"null" (文字字符串)。

What you have to do is propbably remove the key from the file altogether (don't add a spotConfig key in the file) to get null 您所要做的可能是从文件中完全删除密钥 (不要在文件中添加spotConfig密钥)以获取null

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

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