Reprint: Java Basic Learning Summary--java Read Properties file summary

Source: Internet
Author: User

Java Basic Learning Summary--java Read properties file summary One, Java Read 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 of the properties file. * Time: 2014/4/2 */package propertiesfile.read.test;import Java.io.bufferedinputstream;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.ioexception;import Java.io.inputstream;import Java.text.messageformat;import Java.util.properties;public class PropertiesFileReadTest {/** * @param args */public static void main (string[] args) {try {Readpropfilebygetres            Ourceasstream ();            System.out.println ("");        Readpropfilebyinputstream (); } catch (Exception e) {e.printstacktrace ();//Todo:handle Exception}}/** * using Getresourcea The Sstream method reads the properties file */static void Readpropfilebygetresourceasstream () {/** * reads src below Config.pro         Perties the configuration file within the package * Test1.properties in Config.properties package */InputStream in1 = PropertiesFileReadTest.class.getClassLoader ()        . getResourceAsStream ("config/properties/test1.properties"); /** * Read and Propertiesfilereadtest class in the same package inside the configuration file * Test2.properties and Propertiesfilereadtest class in the same package *                            /InputStream in2 = Propertiesfilereadtest.class. getResourceAsStream ("test2.properties");         When the class file and the properties file are in the same directory, the "class name" method is used to read the file without adding getClassLoader (), otherwise it will not get the file. Class.getresourceasstream /** * Read profile of files in src root directory * jdbc.properties in src directory */inputstream in3 = Propertiesfil        EReadTest.class.getClassLoader (). getResourceAsStream ("jdbc.properties");        /** * Read the config file located in another source folder * Config is a source folder, Config.properties is located in the Config source folder */ InputStream in4 = PropertiesFileReadTest.class.getClassLoader (). GetresourCeasstream ("Config.properties");        Properties P = new properties ();        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"));//%s Here is Java String accounted for           Bit characters 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 block E.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 ();         }}}}/** * Read the properties file using the InputStream stream */static void Readpropfilebyinputstream () { 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 config file for file under src root * jdbc.properties in src directory */in1 =            New Bufferedinputstream (New FileInputStream ("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 (New FileInputStream ("Src/config/properties/test1.properties")); /** * Read and Propertiesfilereadtest classes in the same package inside the config file * test2.properties and Propertiesfilereadtes The T class is inside the same package */in3 = new Bufferedinputstream ("New FileInputStream" ("Src/propertiesf            Ile/read/test/test2.properties ")); /** * Read the config file located inside another source folder * ConFig is a source folder, config.properties in the Config source folder */in4 = new FileInputStream ("Config/config.pr            Operties ");            Properties P = new properties ();            System.out. println ("----------------------------------------------");            P.load (in1);            System.out.println ("jdbc.properties:"); System.out.println (String.Format ("jdbc.driver=%s", P.getproperty ("Jdbc.driver"));//%s Here is Java String accounted for            Character 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! = 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 (); }            }        }    }}

Operation Result:

Reprint: Java Basic Learning Summary--java Read Properties file summary

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.