Java Way to read properties

Source: Internet
Author: User
Tags assert locale
The properties files are often read in daily use, and some of the Java read properties that you collect online are summarized as follows:

1, using the Java.util.Properties class of the load () method
Such as:
InputStream is = new Bufferedinputstream (new FileInputStream (the path to the properties file);
Properties Properties = new properties ();
Properties.load (IS);
2, the use of Java.util.ResourceBundle class Getbundle () method
Such as:
ResourceBundle RB = Resourcebundle.getbundle (name, Locale.getdefault ());
3, the use of the Java.util.PropertyResourceBundle class of constructors
Such as:
InputStream is = new Bufferedinputstream (new FileInputStream (name));
ResourceBundle RB = new propertyResourceBundle (IS);
4, the use of class variables getResourceAsStream () method
Such as:
InputStream in = class name. Class.getresourceasstream (name);
Properties P = new properties ();
P.load (in);
5. The getResourceAsStream () method of Java.lang.ClassLoader obtained by using Class.getclassloader ()
Such as:
InputStream in = class name. Class.getclassloader (). getResourceAsStream (name);
Properties P = new properties ();
P.load (in);

6, using the Java.lang.ClassLoader class of Getsystemresourceasstream () static method
Such as:

InputStream in = Classloader.getsystemresourceasstream (name);
Properties P = new properties ();
P.load (in);

Here are some code

Resourcebundleadapter class:

Package com.dengsilinming.read.properties;
Import java.util.Enumeration;
Import Java.util.Locale;
Import java.util.Properties;
Import Java.util.PropertyResourceBundle;

Import Java.util.ResourceBundle; /** * TODO Comment of Resourcebundleadapter * * @author dengsilinming * @version $Id: Resourcebundleadapter.java 2013 -1-30 PM 1:35:47 $/public class Resourcebundleadapter extends Properties {/** * */private static F

    inal long serialversionuid = 1L;
        Public Resourcebundleadapter (ResourceBundle RB) {assert (RB instanceof Java.util.PropertyResourceBundle);
        THIS.RB = RB;
        Enumeration<string> e = Rb.getkeys ();
            while (E.hasmoreelements ()) {Object o = e.nextelement ();
        This.put (O, Rb.getobject ((String) o));

    } private ResourceBundle RB = null;
    Public ResourceBundle Getbundle (String baseName) {return resourcebundle.getbundle (baseName); Public ResourceBundle GETbundle (String baseName, Locale Locale) {return Resourcebundle.getbundle (BaseName, Locale); ResourceBundle Getbundle (String baseName, Locale Locale, ClassLoader loader) {return resourcebundle
    . Getbundle (baseName, locale, loader);
    Public enumeration<?> Getkeys () {return Rb.getkeys ();
    Public Locale GetLocale () {return Rb.getlocale ();
    Public Object GetObject (String key) {return rb.getobject (key);
    public string getString (string key) {return rb.getstring (key);
    String[] Getstringarray (String key) {return Rb.getstringarray (key);
    } protected Object Handlegetobject (String key) {return (propertyResourceBundle) RB). Handlegetobject (key); }
}

Operateproperties class:

Package com.dengsilinming.read.properties;
Import Java.io.BufferedInputStream;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.Enumeration;
Import Java.util.Locale;
Import java.util.Properties;
Import Java.util.PropertyResourceBundle;


Import Java.util.ResourceBundle; /** * TODO Comment of jproperties * * @author dengsilinming * @version $Id: Jproperties.java 2013-1-30 1:34:32 $ *
    /public class Operateproperties {public final static int by_properties = 1;
    Public final static int by_resourcebundle = 2;
    Public final static int by_propertyresourcebundle = 3;
    Public final static int by_class = 4;
    Public final static int by_classloader = 5;

    Public final static int by_system_classloader = 6;  Public final static Properties Loadproperties (final String name, final int type) throws IOException {properties P
        = new Properties (); InputStream in = null;
            if (type = = by_properties) {in = new Bufferedinputstream (new FileInputStream (name));
            ASSERT (in!= null);
        P.load (in);
            else if (type = = By_resourcebundle) {ResourceBundle RB = Resourcebundle.getbundle (name, locale.english);
            ASSERT (RB!= null);
        p = new Resourcebundleadapter (RB);
            else if (type = = By_propertyresourcebundle) {in = new Bufferedinputstream (new FileInputStream (name));
            ASSERT (in!= null);
            ResourceBundle RB = new propertyResourceBundle (in);
        p = new Resourcebundleadapter (RB);
            else if (type = = By_class) {assert (OperateProperties.class.equals (new Operateproperties (). GetClass ()));
            in = OperateProperties.class.getResourceAsStream (name);
            ASSERT (in!= null);
            P.load (in);
        return new Jproperties (). GetClass (). getResourceAsStream (name); } ELSE if (type = = By_classloader) {assert (OperateProperties.class.getClassLoader (). Equals (New Operatepropertie
            S (). GetClass (). getClassLoader ());
            in = OperateProperties.class.getClassLoader (). getResourceAsStream (name);
            ASSERT (in!= null);
            P.load (in);
        return new Jproperties (). GetClass (). getClassLoader (). getResourceAsStream (name);
            else if (type = = By_system_classloader) {in = Classloader.getsystemresourceasstream (name);
            ASSERT (in!= null);
        P.load (in);
        } if (in!= null) {in.close ();
    } return p; public static class Resourcebundleadapter extends Properties {/** * * */PRIV

        Ate static final long serialversionuid = 1L;
            Public Resourcebundleadapter (ResourceBundle RB) {assert (RB instanceof Java.util.PropertyResourceBundle);
            THIS.RB = RB; EnumeratiOn<string> e = Rb.getkeys ();
                while (E.hasmoreelements ()) {Object o = e.nextelement ();
            This.put (O, Rb.getobject ((String) o));

        } private ResourceBundle RB = null;
        Public ResourceBundle Getbundle (String baseName) {return resourcebundle.getbundle (baseName); ResourceBundle Getbundle (String baseName, Locale Locale) {return Resourcebundle.getbundle (b
        Asename, Locale); ResourceBundle Getbundle (String baseName, Locale Locale, ClassLoader loader) {return resour
        Cebundle.getbundle (baseName, locale, loader);
        Public enumeration<?> Getkeys () {return Rb.getkeys ();
        Public Locale GetLocale () {return Rb.getlocale ();
        Public Object GetObject (String key) {return rb.getobject (key); public string getString (string Key) {return rb.getstring (key);
        String[] Getstringarray (String key) {return Rb.getstringarray (key); } protected Object Handlegetobject (String key) {return ((propertyResourceBundle) RB). Handlegetobject
        (key); }
    }
}

Propertiestest class:

Package com.dengsilinming.read.properties;

Import java.util.Properties;

Import Junit.framework.TestCase; /** * TODO Comment of Propertiestest * * @author dengsilinming * @version $Id: Propertiestest.java 2013-1-30 afternoon 1:40:5
    2 $ */public class Propertiestest extends TestCase {operateproperties jproperties;
    String key = "Helloworld.title";

    String value = "Hello world!";
        public void Testloadproperties () throws Exception {String name = null;

        Properties P = new properties ();
        Name = "F:/workspace/dailypractise/src/com/dengsilinming/read/properties/localeproperties.properties";
        p = operateproperties.loadproperties (name, operateproperties.by_properties);

        Assertequals (value, P.getproperty (key));
        Name = "Com.dengsilinming.read.properties";
        p = operateproperties.loadproperties (name, operateproperties.by_resourcebundle);
        Assertequals (value, P.getproperty (key)); Assertequals (Value,((Operateproperties.resourcebundleadapter) p). GetString (key));
        Name = "F:/workspace/dailypractise/src/com/dengsilinming/read/properties/localeproperties.properties";
        p = operateproperties.loadproperties (name, operateproperties.by_propertyresourcebundle);
        Assertequals (value, P.getproperty (key));

        Assertequals (value, (Operateproperties.resourcebundleadapter) p). GetString (key));
        Name = "\\com\\dengsilinming\\read\\properties\\localeProperties.properties";
        p = operateproperties.loadproperties (name, Operateproperties.by_system_classloader);

        Assertequals (value, P.getproperty (key));
        Name = "\\com\\dengsilinming\\read\\properties\\localeProperties.properties";
        p = operateproperties.loadproperties (name, Operateproperties.by_classloader);

        Assertequals (value, P.getproperty (key));
        Name = "\\properties\\localeProperties.properties"; p = operateproperties.loadproperties (name, Operateproperties.by_clasS);
    Assertequals (value, P.getproperty (key)); }
}

The attached code in the runtime is the error, everyone to modify it, are some very easy questions, but do not lazy oh

The point to note is this: this is an article I read on the Internet, I have slightly changed some of the code, because I did not pay much attention to the origin of the article, so I apologize to the original author

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.