One, Java read the properties file summary
In a Java project, the operation of the properties file is often done, because a lot of configuration information will be written in the properties file, here is the main summary of the use of the getResourceAsStream method and InputStream Stream to read the properties file, use the getResourceAsStream method to read the properties file, you need to pay special attention to the properties file path, The test items are as follows:
1.1. Directory structure of the project
1.2. Java read the Properties file code test
/*Example name: Java Read Properties File Summary * source file name: Propertiesfilereadtest.java * Highlights: * 1. Use the getResourceAsStream method to read the properties file * 2. Use the InputStream stream to read the properties file * 3. To read the path to the properties file. * Date: 2014/4/2*/PackagePropertiesFile.read.test;ImportJava.io.BufferedInputStream;ImportJava.io.FileInputStream;ImportJava.io.FileNotFoundException;ImportJava.io.IOException;ImportJava.io.InputStream;ImportJava.text.MessageFormat;ImportJava.util.Properties;PublicClasspropertiesfilereadtest {/***@paramArgs*/PublicStaticvoidMain (string[] args) {Try{Readpropfilebygetresourceasstream (); System.out.println (""); Readpropfilebyinputstream (); }Catch(Exception e) {E.printstacktrace ();//Todo:handle exception} }/*** Use the getResourceAsStream method to read the properties file*/StaticvoidReadpropfilebygetresourceasstream () {/*** Read the configuration file within the Config.properties package under SRC * test1.properties located in the Config.properties package*/InputStream in1 = propertiesfilereadtest.Class. getClassLoader (). getResourceAsStream ("Config/properties/test1.properties");/*** Read and Propertiesfilereadtest classes in the same package inside the config file * test2.properties and Propertiesfilereadtest classes in the same package*/InputStream in2 = propertiesfilereadtest.Class. getResourceAsStream ("Test2.properties");/*** Read the profile of the file under the SRC root directory * jdbc.properties in the SRC directory*/InputStream in3 = propertiesfilereadtest.Class. getClassLoader (). getResourceAsStream ("Jdbc.properties");/*** Read the config file located in another source folder * Config is a source folder, config.properties in the Config source folder*/InputStream in4 = propertiesfilereadtest.Class. getClassLoader (). getResourceAsStream ("Config.properties"); Properties p =NewProperties (); SYSTEM.OUT.PRINTLN ("----Use the getResourceAsStream method to read the properties file----");Try{System.out. println ("----------------------------------------------"); P.load (in1); System.out.println ("test1.properties:name=" + p.getproperty ("name")) + ", age=" + p.getproperty ("Age")); System.out. println ("----------------------------------------------"); P.load (in2); System.out.println ("test2.properties:name=" + p.getproperty ("name")) + ", age=" + p.getproperty ("Age")); System.out. println ("----------------------------------------------"); P.load (in3); System.out.println ("Jdbc.properties:"); System.out.println (String.Format ("jdbc.driver=%s", P.getproperty ("Jdbc.driver"));//The%s here is the Java String placeholder System.out.println (String.Format ("jdbc.url=%s", P.getproperty ("Jdbc.url"))); System.out.println (String.Format ("jdbc.usename=%s", P.getproperty ("Jdbc.usename"))); System.out.println (String.Format ("jdbc.password=%s", P.getproperty ("Jdbc.password"))); System.out. println ("----------------------------------------------"); P.load (IN4); System.out.println ("Config.properties:"); System.out.println (Messageformat.format ("dbuser={0}", P.getproperty ("Dbuser"));//{0} is a Java string placeholder System.out.println (Messageformat.format ("dbpassword={0}", P.getproperty ("Dbpassword"))); System.out.println (Messageformat.format ("database={0}", P.getproperty ("Database"))); System.out. println ("----------------------------------------------"); }Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }Finally{if (in1! =Null) {Try{In1.close ();}Catch(IOException e) {E.printstacktrace ();} }if (in2! =Null) {Try{In2.close ();}Catch(IOException e) {E.printstacktrace ();} }if (in3! =Null) {Try{In3.close ();}Catch(IOException e) {E.printstacktrace ();} }if (in4! =Null) {Try{In4.close ();}Catch(IOException e) {E.printstacktrace ();} } } }/*** Use InputStream stream to read the properties file*/StaticvoidReadpropfilebyinputstream () {InputStream in1 =Null; InputStream in2 =Null; InputStream in3 =Null; InputStream in4 =Null; SYSTEM.OUT.PRINTLN ("----reads the properties file----using the InputStream stream");Try{/*** Read the profile of the file under the SRC root directory * jdbc.properties in the SRC directory*/in1 =New Bufferedinputstream (NewFileInputStream ("Src/jdbc.properties"));/*** Read the configuration file within the Config.properties package under SRC * test1.properties located in the Config.properties package*/in2 =New Bufferedinputstream (NewFileInputStream ("Src/config/properties/test1.properties"));/*** Read and Propertiesfilereadtest classes in the same package inside the config file * test2.properties and Propertiesfilereadtest classes in the same package*/in3 =New Bufferedinputstream (NewFileInputStream ("Src/propertiesfile/read/test/test2.properties"));/*** Read the config file located in another source folder * Config is a source folder, config.properties in the Config source folder*/IN4 =New FileInputStream ("Config/config.properties"); Properties p =NewProperties (); System.out. println ("----------------------------------------------"); P.load (in1); System.out.println ("Jdbc.properties:"); System.out.println (String.Format ("jdbc.driver=%s", P.getproperty ("Jdbc.driver"));//The%s here is the Java String placeholder System.out.println (String.Format ("jdbc.url=%s", P.getproperty ("Jdbc.url"))); System.out.println (String.Format ("jdbc.usename=%s", P.getproperty ("Jdbc.usename"))); System.out.println (String.Format ("jdbc.password=%s", P.getproperty ("Jdbc.password"))); System.out. println ("----------------------------------------------"); P.load (in2); System.out.println ("test1.properties:name=" + p.getproperty ("name")) + ", age=" + p.getproperty ("Age")); System.out. println ("----------------------------------------------"); P.load (in3); System.out.println ("test2.properties:name=" + p.getproperty ("name")) + ", age=" + p.getproperty ("Age")); System.out. println ("----------------------------------------------"); P.load (IN4); System.out.println ("Config.properties:"); System.out.println (Messageformat.format ("dbuser={0}", P.getproperty ("Dbuser"));//{0} is a Java string placeholder System.out.println (Messageformat.format ("dbpassword={0}", P.getproperty ("Dbpassword"))); System.out.println (Messageformat.format ("database={0}", P.getproperty ("Database"))); System.out. println ("----------------------------------------------"); }Catch(FileNotFoundException e) {E.printstacktrace ();}Catch(IOException e) {E.printstacktrace ();}Finally{if (in1! =Null) {Try{In1.close ();}Catch(IOException e) {E.printstacktrace ();} }if (in2! =nulltry {in2.close ();} catch (IOException e) {e.printstacktrace ();}} if (in3! = nulltry< Span style= "color: #000000;" > {in3.close ();} catch (IOException e) {e.printstacktrace ();}} if (in4! = nulltry< Span style= "color: #000000;" > {in4.close ();} catch (IOException e) {e.printstacktrace ();}}} }}
Operation Result:
Java Basic Learning Summary--java Read Properties file summary