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, using the getResourceAsStream method to read the properties file requires special Note The properties file path, the test project is as follows:
/*Example Name: Java Read Properties File Summary * source file name: Propertiesfilereadtest.java * Highlights: * 1. Use the getResourceAsStream method to read Properties File * 2. Use the InputStream stream to read the properties file * 3. To read the path to the properties file writing questions * **/ PackagepropertiesFile.read.test;ImportJava.io.BufferedInputStream;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.text.MessageFormat;Importjava.util.Properties; Public classPropertiesfilereadtest { Public Static voidMain (string[] args)throwsFileNotFoundException {readpropfilebygetresourceasatream (); System.out.println ("--------------"); //Readpropfilebyinputstream (); } /** Read using the Getresourceasatream method*/ Private Static voidReadpropfilebygetresourceasatream () {/** Read the configuration file within the Config.properties package under SRC test1.properties located in the Config.properties package*/InputStream INL= 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 within 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 config file in another source folder config is a source folder, config.properties in config * source folder*/InputStream in4= Propertiesfilereadtest.class. getClassLoader (). getResourceAsStream ("Config.properties"); Properties Properties=NewProperties (); System.out.println ("----Use the getResourceAsStream method to read the properties file----"); //reads the list of attributes from the input byte stream (key, value) Try{System.out.println ("-----------------------"); Properties.load (INL); System.out.println ("Test1.properties:name=" + properties.getproperty ("name") + ", age=" + properties.getproperty ("Age")); System.out.println ("-----------------------"); System.out.println ("-----------------------"); Properties.load (in2); System.out.println ("Test2.properties:name=" + properties.getproperty ("name") + ", age=" + properties.getproperty ("Age")); System.out.println ("-----------------------"); Properties.load (in3); System.out.println ("Jdbc.properties:"); //returns the formatted string using the specified format string and parameters, where%s is the Java string placeholderSystem.out.println (String.Format ("jdbc.url=%s", Properties.getproperty ("Jdbc.url"))); System.out.println (String.Format ("jdbc.usename=%s", Properties.getproperty ("Jdbc.usename"))); System.out.println (String.Format ("jdbc.password=%s", Properties.getproperty ("Jdbc.password"))); Properties.load (IN4); System.out.println ("Config.properties:"); //creates a messageformat using the given pattern and uses it to format the given parameter, {0} is a Java string placeholderSystem.out.println (Messageformat.format ("dbuser={0}", Properties.getproperty ("Dbuser"))); System.out.println (Messageformat.format ("Dbpassword={0}", Properties.getproperty ("Dbpassword"))); System.out.println (Messageformat.format ("Database={0}", Properties.getproperty ("Database"))); System.out.println ("----------------------------------------------"); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally { if(INL! =NULL) { Try{inl.close (); } Catch(IOException e) {e.printstacktrace (); } } if(In2! =NULL) { Try{inl.close (); } Catch(IOException e) {e.printstacktrace (); } } if(in3! =NULL) { Try{inl.close (); } Catch(IOException e) {e.printstacktrace (); } } if(In4! =NULL) { Try{inl.close (); } Catch(IOException e) {e.printstacktrace (); } } } }
/** Use InputStream stream to read properties*/ Private Static voidReadpropfilebyinputstream ()throwsfilenotfoundexception {InputStream in1=NULL; InputStream in2=NULL; InputStream in3=NULL; InputStream in4=NULL; System.out.println ("----use InputStream stream to read the properties file----"); Try { /** Read the configuration file within the Config.properties package under SRC test1.properties located in the Config.properties package*/in1=NewBufferedinputstream (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 */in2=NewBufferedinputstream (NewFileInputStream ("Src/propertiesfile/read/test/test2.properties")); /** Read the profile of the file under the SRC root directory * jdbc.properties in the SRC directory*/in3=NewBufferedinputstream (NewFileInputStream ("Src/jdbc.properties")); /** Read the config file located in another source folder * Config is a source folder, config.properties in the config source folder*/in4=NewFileInputStream ("Config/config.properties"); Properties Properties=NewProperties (); System.out.println ("-----------------------"); Properties.load (in1); System.out.println ("Test1.properties:name=" + properties.getproperty ("name") + ", age=" + properties.getproperty ("Age")); System.out.println ("-----------------------"); System.out.println ("-----------------------"); Properties.load (in2); System.out.println ("Test2.properties:name=" + properties.getproperty ("name") + ", age=" + properties.getproperty ("Age")); System.out.println ("-----------------------"); Properties.load (in3); System.out.println ("Jdbc.properties:"); //returns the formatted string using the specified format string and parameters, where%s is the Java string placeholderSystem.out.println (String.Format ("jdbc.url=%s", Properties.getproperty ("Jdbc.url"))); System.out.println (String.Format ("jdbc.usename=%s", Properties.getproperty ("Jdbc.usename"))); System.out.println (String.Format ("jdbc.password=%s", Properties.getproperty ("Jdbc.password"))); Properties.load (IN4); System.out.println ("Config.properties:"); //creates a messageformat using the given pattern and uses it to format the given parameter, {0} is a Java string placeholderSystem.out.println (Messageformat.format ("dbuser={0}", Properties.getproperty ("Dbuser"))); System.out.println (Messageformat.format ("Dbpassword={0}", Properties.getproperty ("Dbpassword"))); System.out.println (Messageformat.format ("Database={0}", Properties.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 (); } } } }
Java Read Properties File summary