Saturday, 15 March 2014

java - Store properties file in map -



java - Store properties file in map -

i want read usual properties file , set attribute , value map object. technically, properties file like

attr1=hello attr2=java attr3=world

would dynamically become map object this

+-------+-------+ + k | v + +-------+-------+ + attr1 | hello + +-------+-------+ + attr2 | java + +-------+-------+ + attr3 | world + +-------+-------+

in end, shouldn't matter what's in properties file, whole file saved map. possible java.properties or filereader or inputstream special parsing necessary?

java.util.properties implements of map.

to load properties file, utilize properties' load() method reader or inputstream.

for instance,

properties config = new properties(); seek { config.load(<this.class>.getresourceasstream(<property-file-name>)); //example input stream //now can access map instance config.get(<key>); config.entryset(); //additionally, can access properties methods config.getproperty(<property-name>); } catch(...) { ... }

if want map instance, let's kind of map<string, string>, need convert properties required map instance iterating property names.

properties config = new properties(); seek { config.load(<this.class>.getresourceasstream(<property-file-name>)); //exampl input stream map<string, string> map = new hashmap<string, string>(); (final string name: config.stringpropertynames()) map.put(name, config.getproperty(name)); } catch(...) { ... }

java map properties config

No comments:

Post a Comment