A properties file is a configuration resource that stores information in the form of key/value pairs.
This code reads and writes a properties file:
// Read the property file Properties props = new Properties(); FileInputStream in = new FileInputStream("app.properties"); props.load(in); String pool = props.getProperty("sys.pool"); String alarm = props.getProperty("sys.alarm"); in.close(); // Show the properties System.out.printf("Pool:%s Alarm:%s", pool, alarm); // Write to the property file props.setProperty("net.roundrobin", "yes"); // New property props.store(new FileOutputStream("app.properties"),"Demonstration");
Where the properties file contains:
sys.alarm=no
sys.pool=5
If you need to iterate on all the keys, you can loop:
for(String key : props.stringPropertyNames()) ...
The JVM has its own properties that describe the environment. Retrieving and adding system properties is quite easy:
String version = System.getProperty("java.version"); System.setProperty("test.props", "123");
Copyright © 2013 Welcome to the website of Davis Fiore. All Rights Reserved.