1

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?

Laur Ivan
  • 4,117
  • 3
  • 38
  • 62
  • 2
    This all looks good. Something else must be wrong. Show the code to load the properties and the two calls to get/getProperty(). – Aaron Digulla Feb 13 '12 at 16:44
  • 1
    `getProperty(key)` returns the `super.get(key)` result unless it is a non-String. Then it attempts to fetch data from the `defaults`. Check that `get()` returns a String object for you, otherwise there's something wrong with your input. – Alexander Pavlov Feb 13 '12 at 16:49
  • @AlexanderPavlov Yep, on the spot. – Laur Ivan Feb 13 '12 at 17:03
  • @AaronDigulla A Properties wrapper I was calling messed that up :( Thanks a mill! Alexander's answer complemented yours perfectly :) – Laur Ivan Feb 13 '12 at 17:04

1 Answers1

1

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
limc
  • 39,366
  • 20
  • 100
  • 145
  • The two comments above pointed to the right answer. Your test is correct, mine was wrapping the java `Properties` class with some extra lookups. – Laur Ivan Feb 13 '12 at 17:07