1 Loading the configuration file
Write the configuration file config.properties in the SRC directory of the Common Java project (if MAVEN works in the project's Src/main/resources) directory
Config.properties
hibernate.dialect=org.hibernate.dialect.OracleDialectdriverClassName= Oracle.jdbc.driver.OracleDriverjdbc_url=jdbc:oracle:thin:@127.0. 0.1:1521: orcljdbc_username=Wangjdbc_password=123456
Propertiestool.java
Public classPropertiestool {PrivateProperties props =NULL; Public voidLoadprop (String configpath) {InputStream InputStream= Object.class. getResourceAsStream (Configpath); Props=NewProperties (); Try{props.load (InputStream); } Catch(Exception e) {e.printstacktrace (); Throw NewRuntimeException ("couldn ' t find properties file,please check it."); } } Publicstring ReadValue (String key) {String value=NULL; if(NULL!=props) {Value=Props.getproperty (key); } returnvalue; }}
Run the test file
1 Read config.properties under src root file
Public Static voidMain (string[] args) {Propertiestool Proptool=NewPropertiestool (); String Configpath="/config.properties"; Proptool.loadprop (Configpath); String value= Proptool.readvalue ("Hibernate.dialect"); System. out. println ("value="+value);}
2 read config.properties in the Com/xinping/service directory under the SRC root directory
Public Static voidMain (string[] args) {Propertiestool Proptool=NewPropertiestool (); String Configpath="/com/xinping/service/config.properties"; Proptool.loadprop (Configpath); String value= Proptool.readvalue ("Hibernate.dialect"); System. out. println ("value="+value); }
Java Development Tips