views:

274

answers:

6

Hi,

the title really says it all. I have a properties file that has lots of different properties in it for different things:

ui.datasource.st.name=MyTest
ui.datasource.st.port=111
ui.datasource.st.ip=1.1.1.1
ui.outputtype.snapshot=Snapshot
ui.outputtype.spreadsheet=Spreadsheet - xls

The file is a lot bigger than this an I want to jump to the ui.outputtype section without looping through the file and checking the key value.

Is there a way to do this?

Cheers Nathan

+1  A: 

Does Properties.getProperty(String key) not do what you want?

Are you trying to retrieve all values whose key starts with a given string?

matt b
A: 

No.

Mmm, I need at least 15 chars to post... So let's expand on the answer.

I don't see the problem. Java has no magical way to guess where your information is. And unless the file is weighting several megabytes, loading it whole (as Properties does, or so I think), and perhaps eliminating data you don't need (from the generated hash table), shouldn't take that much time. At worse, if memory is an issue, parse it manually line per line, shouldn't be hard and should be still quite fast.

If memory isn't the problem, looping with the enumeration provided by propertyNames() and checking that the keys startWith() the given string shouldn't take much time either.

PhiLho
+2  A: 

You should lode the properties, and then read by key:

Properties props = new Properties();
props.load(new FileInputStream("my.properties"));

props.getProperty("ui.outputtype.snapshot");
Zed
A: 

Since there is no concept of a section in the Properties class, one would have to come up with some way to find the section of the property file which is wanted.

One possible approach would be to obtain the Set of keys, and find the ones which contains the prefix which is wanted.

This would be like applying a filter on the keys of the property and picking out the keys which are desired. In this case, we can think of the prefix as being the dot-separated entry in the property file.

Here's a little sample code to demonstrate the overall idea:

Properties p = new Properties();
p.put("hello.world", "42");
p.put("hello.earth", "1");
p.put("abc.def", "3");
p.put("def.ghi", "5");

// Go through the keys in the property.
for (Object propKey : p.keySet()) {
  String key = (String)propKey;

  // Select the keys with the prefix "hello."
  if (key.startsWith("hello.")) {
    System.out.println(key + ", " + p.getProperty(key));
  }
}

(The code is not very pleasant because Properies is not genericized.)

Output:

hello.world, 42
hello.earth, 1

In the example above, the one did not load the entries from the an external properties file, but that can be achieved trivially using the Properties.load method, as previously mentioned in the other answers.


A little bit of a different approach, where the keys are filtered by the desired prefix, then the information can be read through a simple for loop.

Here's an example using the Collections2.filter method from the Google Collections, which can filter a Collection by a certain Predicate. The filtered result (keys which have the desired prefix) is given to the for loop in order to obtain the key and value pair:

Properties p = new Properties();
p.put("hello.world", "42");
p.put("hello.earth", "1");
p.put("abc.def", "3");
p.put("def.ghi", "5");

for (Object propKey : Collections2.filter(p.keySet(), new Predicate<Object>() {
  public boolean apply(Object o) {
    return ((String)o).startsWith("hello.");
  }
}))
{
  String key = (String) propKey;
  System.out.println(key + ", " + p.getProperty(key));
}

This may be a little bit overkill, but it's another approach to implementing a filter to narrow down the properties with the desired keys.

coobird
A: 

This is a strange requirement. If you are worried about performance then do this before when packaging not at runtime.

A simple:

cat settings.properties | grep ui.outputtype > mysmaller.properties

should suffice!

Pablojim
A: 

Hi there, sorry for not dropping by sooner - I hadn't realised there wer any comments/answers here.

Basically I have 'sections' in my properties file which was developed by a colleague, I was thinking of one file per 'section'. Anyway, my understanding of how this all works is growing niocely and the colution I came up with was:

for(String key : props.stringPropertyNames())
 {
  // Jump to the relevant section of a properties file
  if(key.contains("datamode"))
  {
   OptionItem oi = new OptionItem(key, props.getProperty(key));
   // String value = props.getProperty(key);
   this.cbo_mode.add(oi);
  }
 }

Thanks for all your help and advice. Nathan

nathj07