import java.io.*; import java.util.*; public class Props { private Properties props = new Properties(); private static Props sysProps; public Props(String filename) { try { InputStream input = getClass().getResourceAsStream(filename); props.load(input); } catch (Exception e) { e.printStackTrace(); } } public String getValue(String key) { if (key.equals("default")) { //return when value is null. return props.getProperty(key, "default"); } return props.getProperty(key); } public String getValueAndPrint(String key) { String value = getValue(key); System.out.println(key + ":" + value); return value; } public void list() { props.list(System.out); } public static String getSysValue(String key) { if (sysProps == null) { sysProps = new Props("config.properties"); } return sysProps.getValue(key); } public static void main(String[] args) { Props props = new Props("config.properties"); props.list(); props.getValueAndPrint("null"); props.getValueAndPrint("name"); props.getValueAndPrint("age"); props.getValueAndPrint("novalue"); System.out.println(getSysValue("null")); System.out.println(getSysValue("name")); System.out.println(getSysValue("age")); System.out.println(getSysValue("novalue")); } }