- Java code
/*TestProperties*/import java.util.Properties;import java.util.Enumeration;public class TestProperties{ public static void main(String[]args) { Properties props=System.getProperties(); Enumeration prop_names=props.propertyNames(); while(prop_names.hasMoreElements()) { String prop_name=(String)prop_names.nextElement(); String property=props.getProperty(prop_name); System.out.println("属性:"+prop_name+"'是"+property+"'"); } }}
------解决方案--------------------
Properties不就是这么用的吗?通常从ini或者xml里面读入程序需要的初始参数。
另外,ResourceBundle的2个子类也蛮好用的。
------解决方案--------------------
- Java code
private ConnectionFactory() { Properties prop = null; try { InputStream in = this.getClass().getClassLoader() .getResourceAsStream("properties/dbconfig.properties"); prop = new Properties(); prop.load(in); driver = prop.getProperty("driver"); dbURL = prop.getProperty("url"); user = prop.getProperty("user"); password = prop.getProperty("password"); in.close(); } catch (Exception e) { System.out.println("No dbconfig.properties defined error"); System.exit(-1); } }