java.util Class Properties java.lang.Object | +--java.util.Dictionary | +--java.util.Hashtable | +--java.util.PropertiesDirect Known Subclasses: Provider --------------------------------------------------------------------------------public class Properties extends Hashtable The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string. A property list can contain another property list as its "defaults"; this second property list is searched if the property key is not found in the original property list. Because Properties inherits from Hashtable, the put and putAll methods can be applied to a Properties object. Their use is strongly discouraged as they allow the caller to insert entries whose keys or values are not Strings. The setProperty method should be used instead. If the store or save method is called on a "compromised" Properties object that contains a non-String key or value, the call will fail.
Class Properties
java.lang.Object
|
+--java.util.Dictionary
|
+--java.util.Hashtable
|
+--java.util.PropertiesDirect Known Subclasses:
Provider --------------------------------------------------------------------------------public class Properties
extends Hashtable
The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string. A property list can contain another property list as its "defaults"; this second property list is searched if the property key is not found in the original property list. Because Properties inherits from Hashtable, the put and putAll methods can be applied to a Properties object. Their use is strongly discouraged as they allow the caller to insert entries whose keys or values are not Strings. The setProperty method should be used instead. If the store or save method is called on a "compromised" Properties object that contains a non-String key or value, the call will fail.
FileInputStream in=null;
Properties props=new Properties(); System.out.println(System.getProperty("user.dir")); String fileName="info.properties";
try{
in=new FileInputStream(fileName);
}catch(FileNotFoundException fnf)
{
fnf.printStackTrace();
return;
}
try{
props.load(in);
}catch(IOException ioex)
{
ioex.printStackTrace();
return;
} String driver=props.getProperty("jdbc.driver","oracle.jdbc.driver.OracleDriver");
String url=props.getProperty("jdbc.url","jdbc:oracle:thin:@localhost:1521");
String user=props.getProperty("jdbc.user","system");
String password=props.getProperty("jdbc.password","manager");
属性文件(info.properties)如下:
jdbc.driver oracle.jdbc.driver.OracleDriver
jdbc.url jdbc:oracle:thin:@localhost:1521
jdbc.user system
jdbc.password manager
只要将String fileName="info.properties";
改成String fileName="info.ini";即可