Java Read Properties configuration file

Source: Internet
Author: User

today do a read the properties file in the configuration, excerpt a little online example.
Finally succeeded, wrote down, to make a memorial

/*** @ Author WHS * @ Creation Date January 8, 2015 * @ Version V 1.0*/ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;Importjava.util.Enumeration;Importjava.util.Properties; Public classTest {/*you can call the class name directly to read the contents of the Properties property file*/ Public voidInitfile () {String URL= This. GetClass (). getClassLoader (). GetResource ("A.properties"). toString (). SUBSTRING (6); String Empurl= Url.replace ("%20", "" "); //If your file path contains spaces, you will be sure to get an error.File File =NewFile (Empurl); FileInputStream FIS=NULL; Try { /*input stream and property file associations*/FIS=Newfileinputstream (file); /*To Create an attribute set object*/Properties prop=NewProperties (); /*loads the read content into the property set object*/prop.load (FIS); /*returns an enumeration of all keys in a property list*/Enumeration<?> enums =Prop.propertynames (); while(Enums.hasmoreelements ()) {/*cast each property to a string type to get the key*/String Key=(String) enums.nextelement (); /*gets the corresponding value according to the key (string type)*/String Value=Prop.getproperty (key); /*output the contents of the Properties property file*/System.out.println (Key+ "----" +value); } } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally { if(FIS! =NULL) Try{fis.close (); } Catch(IOException e) {e.printstacktrace (); } } } Public Static voidMain (string[] args) {/*calling a method to read the contents of a property file*/test test=NewTest (); Test.initfile (); }}
several common ways to read. properties files in online articles

1. Example of the load () method using the Java.util.Properties class :

Java code
    1. InputStream in = new Bufferedinputstream (new FileInputStream (name));
    2. Properties P = new properties ();
    3. P.load (in);
InputStream in = new Bufferedinputstream (new FileInputStream (name)); Properties P = new properties (); P.load (in);

2. Example of the Getbundle () method using the Java.util.ResourceBundle class :

Java code
    1. ResourceBundle RB = Resourcebundle.getbundle (name, Locale.getdefault ());
      

Use ResourceBundle to read. properties files to avoid path problems

When I read the. properties file in the jar, I always can't find the file path and later read the. properties file with ResourceBundle to avoid the path problem, the code is as follows:
Process is the file name, remember not to add. Properties, URL is the key name in the file

Java code
    1. ResourceBundle bundle = Resourcebundle.getbundle ("com.ihandy.smsoc.app.process");
    2. String s = bundle.getstring ("URL");
    3. System.out.println (s);
    4. PURL = s;
      ResourceBundle bundle = Resourcebundle.getbundle ("com.ihandy.smsoc.app.process");      String s = bundle.getstring ("URL");  System.out.println (s);  PURL = s;

3. Examples of constructors using the Java.util.PropertyResourceBundle class :

Java code
    1. InputStream in = new Bufferedinputstream (new FileInputStream (name));
    2. ResourceBundle RB = new propertyResourceBundle (in);

4. Example of the getResourceAsStream () method using class variables:

Java code
    1. InputStream in = class name.   Class.getresourceasstream (name);
    2. Properties P = new properties ();
    3. P.load (in);

5. Example of the Java.lang.ClassLoader getResourceAsStream () method obtained using Class.getclassloader () :

Java code
    1. InputStream in = class name.   Class.getclassloader (). getResourceAsStream (name);
    2. Properties P = new properties ();
    3. P.load (in);

6. Examples of Getsystemresourceasstream () static methods using the Java.lang.ClassLoader class :

Java code
    1. InputStream in = Classloader.getsystemresourceasstream (name);
    2. Properties P = new properties ();
    3. P.load (in);

7. The Javax.servlet.ServletContext getResourceAsStream () method can be used in the servlet example:

Java code
    1. InputStream in = context.getresourceasstream (path);
    2. Properties P = new properties ();
    3. P.load (in);

Java Read Properties configuration file

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.