Creating the Proputil Class
Importjava.io.IOException;ImportJava.io.InputStream;Importjava.util.Properties; Public classProputil {/*** Get config file *@param * @return */ Private StaticProperties Properties =NULL; Publicproputil (String path) {Initialize (path); } Private voidInitialize (String path) {InputStream is=getclass (). getClassLoader (). getResourceAsStream (path); if(IS = =NULL) { return; } Properties=NewProperties (); Try{properties.load (IS); } Catch(IOException e) {}finally { Try { if(Is! =NULL) Is.close (); } Catch(Exception e) {}}} /*** Get specified key in config files * *@paramkey * The key name to get value*/ Publicstring Get (String key) {string KeyValue=NULL; if(Properties.containskey (key)) {KeyValue=(String) properties.get (key); } returnKeyValue; }}
Create a Properties file Framework.properties
Name=linchaojiangage=29code=009045
Create a test class readframeworkproperties, read the framework.properties file
Public classreadframeworkproperties {Private StaticProputil Proputil =NewProputil ("Config/framework.properties"); Public StaticString name = Proputil.get ("name"); Public Static voidMain (string[] args) {//TODO auto-generated Method StubSystem.out.println (name); System.out.println (age); SYSTEM.OUT.PRINTLN (code); }}
Output Result:
Linchaojiang
29
009045
Java Read the properties file