为什么 getProperty() 返回 null?

发布于 2025-01-05 06:31:02 字数 712 浏览 1 评论 0原文

我有一段定义属性的代码,如下所示:

public static final String DEFINED_KEY = "definedKey";
public static final String DEFINED_PROPERTY = "definedProperty";

// [...]

File f = File.createTempFile("default", ".properties");
PrintWriter pw = new PrintWriter(f);

Properties pp = new Properties();
pp.setProperty(DEFINED_KEY, DEFINED_PROPERTY);
pp.store(pw, "Automatically defined");
pw.close();

它保存属性文件 OK

#No comments
#Mon Feb 13 17:25:12 CET 2012
definedKey=definedProperty

当我创建另一个属性并对其执行 load() 时,它加载正常。 get(DEFINED_KEY) 返回为 DEFINED_PROPERTY 指定的值,但 getProperty(DEFINED_KEY) 返回 null。这是怎么回事?

I have a piece of code defining a property like this:

public static final String DEFINED_KEY = "definedKey";
public static final String DEFINED_PROPERTY = "definedProperty";

// [...]

File f = File.createTempFile("default", ".properties");
PrintWriter pw = new PrintWriter(f);

Properties pp = new Properties();
pp.setProperty(DEFINED_KEY, DEFINED_PROPERTY);
pp.store(pw, "Automatically defined");
pw.close();

Which saves a properties file OK

#No comments
#Mon Feb 13 17:25:12 CET 2012
definedKey=definedProperty

When I create another property and perform a load() on it, it loads OK. get(DEFINED_KEY) returns the value specified for DEFINED_PROPERTY, but getProperty(DEFINED_KEY) returns null. What's up with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身泪倾城 2025-01-12 06:31:02

我没有发现您的代码有任何问题...这是我的测试:-

public static final String DEFINED_KEY = "definedKey";
public static final String DEFINED_PROPERTY = "definedProperty";

public void run() throws Exception {
    // your code
    File f = File.createTempFile("default", ".properties");
    PrintWriter pw = new PrintWriter(f);
    Properties pp = new Properties();
    pp.setProperty(DEFINED_KEY, DEFINED_PROPERTY);
    pp.store(pw, "Automatically defined");
    pw.close();

    // examining the generated properties file
    System.out.println("Reading from properties file...");
    System.out.println("------------");
    Scanner scanner = new Scanner(f);
    while (scanner.hasNextLine()) {
        System.out.println(scanner.nextLine());
    }
    System.out.println("------------");

    // loading properties file
    Properties p = new Properties();
    p.load(new FileInputStream(f));

    System.out.println("p.get(DEFINED_KEY): " + p.get(DEFINED_KEY));
    System.out.println("p.getProperty(DEFINED_KEY): " + p.getProperty(DEFINED_KEY));
}

生成的输出:-

Reading from properties file...
------------
#Automatically defined
#Mon Feb 13 11:00:42 CST 2012
definedKey=definedProperty
------------
p.get(DEFINED_KEY): definedProperty
p.getProperty(DEFINED_KEY): definedProperty

I don't see anything wrong with your code... here's my test:-

public static final String DEFINED_KEY = "definedKey";
public static final String DEFINED_PROPERTY = "definedProperty";

public void run() throws Exception {
    // your code
    File f = File.createTempFile("default", ".properties");
    PrintWriter pw = new PrintWriter(f);
    Properties pp = new Properties();
    pp.setProperty(DEFINED_KEY, DEFINED_PROPERTY);
    pp.store(pw, "Automatically defined");
    pw.close();

    // examining the generated properties file
    System.out.println("Reading from properties file...");
    System.out.println("------------");
    Scanner scanner = new Scanner(f);
    while (scanner.hasNextLine()) {
        System.out.println(scanner.nextLine());
    }
    System.out.println("------------");

    // loading properties file
    Properties p = new Properties();
    p.load(new FileInputStream(f));

    System.out.println("p.get(DEFINED_KEY): " + p.get(DEFINED_KEY));
    System.out.println("p.getProperty(DEFINED_KEY): " + p.getProperty(DEFINED_KEY));
}

The generated output:-

Reading from properties file...
------------
#Automatically defined
#Mon Feb 13 11:00:42 CST 2012
definedKey=definedProperty
------------
p.get(DEFINED_KEY): definedProperty
p.getProperty(DEFINED_KEY): definedProperty
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文