Java Read Properties File summary

Source: Internet
Author: User
Tags list of attributes

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.