java 将空字符串设置为属性文件中的键值

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

Set empty string as the value of a key in properties file

javasecurityweb-applicationspropertiespasswords

提问by Susie

jdbc.password=

How can I assign jdbc.passwordkey in my application.properties file an empty string?

如何jdbc.password在我的 application.properties 文件中指定一个空字符串?

I understand I can do this programmatically as follows, but I would like to set this in properties file.

我知道我可以按如下方式以编程方式执行此操作,但我想在属性文件中进行设置。

Properties props = new Properties();
props.put("password", "");

回答by Jon Skeet

Just leaving the value empty on the RHS should be fine:

只需将 RHS 上的值留空就可以了:

password=

Sample code:

示例代码:

import java.io.*;
import java.util.*;

class Test{
    public static void main(String [] args) throws Exception {
        Properties props = new Properties();
        props.load(new StringReader("password="));
        System.out.println(props.size()); // 1
        System.out.println(props.getProperty("password").length()); // 0
    }
}