java读取properties的方法

在日常使用中经常会读取properties文件,现在把自己在网上搜集的一些java读取properties的方法汇总如下:

1、使用java.util.Properties类的load()方法
如: 

[java] view plain copy
 
  1. InputStream is = new BufferedInputStream(new FileInputStream(new File(properties文件所在路径));  
  2. Properties properties = new Properties();  
  3. properties.load(is);  

2、使用java.util.ResourceBundle类的getBundle()方法
如:

[java] view plain copy
 
  1. ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());  

3、使用java.util.PropertyResourceBundle类的构造函数
如: 

[java] view plain copy
 
  1. InputStream is = new BufferedInputStream(new FileInputStream(name));  
  2. ResourceBundle rb = new PropertyResourceBundle(is);  

4、使用class变量的getResourceAsStream()方法
如: 

[java] view plain copy
 
  1. InputStream in = 类名.class.getResourceAsStream(name);  
  2. Properties p = new Properties();  
  3. p.load(in);  

5、使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
如:

[java] view plain copy
 
  1.  InputStream in = 类名.class.getClassLoader().getResourceAsStream(name);  
  2. Properties p = new Properties();  
  3. p.load(in);  

6、使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
如: 

[java] view plain copy
 
  1. InputStream in = ClassLoader.getSystemResourceAsStream(name);  
  2. Properties p = new Properties();  
  3. p.load(in);  

下面附上一些代码

ResourceBundleAdapter类:

[java] view plain copy
 
  1. package com.dengsilinming.read.properties;  
  2.   
  3. import java.util.Enumeration;  
  4. import java.util.Locale;  
  5. import java.util.Properties;  
  6. import java.util.PropertyResourceBundle;  
  7. import java.util.ResourceBundle;  
  8.   
  9. /** 
  10.  * TODO Comment of ResourceBundleAdapter 
  11.  *  
  12.  * @author dengsilinming 
  13.  * @version $Id: ResourceBundleAdapter.java 2013-1-30 下午1:35:47 $ 
  14.  */  
  15. public class ResourceBundleAdapter extends Properties {  
  16.     /** 
  17.      *  
  18.      */  
  19.     private static final long serialVersionUID = 1L;  
  20.   
  21.     public ResourceBundleAdapter(ResourceBundle rb) {  
  22.         assert (rb instanceof java.util.PropertyResourceBundle);  
  23.         this.rb = rb;  
  24.         Enumeration<String> e = rb.getKeys();  
  25.         while (e.hasMoreElements()) {  
  26.             Object o = e.nextElement();  
  27.             this.put(o, rb.getObject((String) o));  
  28.         }  
  29.     }  
  30.   
  31.     private ResourceBundle rb = null;  
  32.   
  33.     public ResourceBundle getBundle(String baseName) {  
  34.         return ResourceBundle.getBundle(baseName);  
  35.     }  
  36.   
  37.     public ResourceBundle getBundle(String baseName, Locale locale) {  
  38.         return ResourceBundle.getBundle(baseName, locale);  
  39.     }  
  40.   
  41.     public ResourceBundle getBundle(String baseName, Locale locale, ClassLoader loader) {  
  42.         return ResourceBundle.getBundle(baseName, locale, loader);  
  43.     }  
  44.   
  45.     public Enumeration<?> getKeys() {  
  46.         return rb.getKeys();  
  47.     }  
  48.   
  49.     public Locale getLocale() {  
  50.         return rb.getLocale();  
  51.     }  
  52.   
  53.     public Object getObject(String key) {  
  54.         return rb.getObject(key);  
  55.     }  
  56.   
  57.     public String getString(String key) {  
  58.         return rb.getString(key);  
  59.     }  
  60.   
  61.     public String[] getStringArray(String key) {  
  62.         return rb.getStringArray(key);  
  63.     }  
  64.   
  65.     protected Object handleGetObject(String key) {  
  66.         return ((PropertyResourceBundle) rb).handleGetObject(key);  
  67.     }  
  68. }  

OperateProperties类:

[java] view plain copy
 
  1. package com.dengsilinming.read.properties;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.FileInputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.util.Enumeration;  
  8. import java.util.Locale;  
  9. import java.util.Properties;  
  10. import java.util.PropertyResourceBundle;  
  11. import java.util.ResourceBundle;  
  12.   
  13.   
  14. /** 
  15.  * TODO Comment of JProperties 
  16.  *  
  17.  * @author dengsilinming 
  18.  * @version $Id: JProperties.java 2013-1-30 下午1:34:32 $ 
  19.  */  
  20. public class OperateProperties {  
  21.     public final static int BY_PROPERTIES             = 1;  
  22.     public final static int BY_RESOURCEBUNDLE         = 2;  
  23.     public final static int BY_PROPERTYRESOURCEBUNDLE = 3;  
  24.     public final static int BY_CLASS                  = 4;  
  25.     public final static int BY_CLASSLOADER            = 5;  
  26.     public final static int BY_SYSTEM_CLASSLOADER     = 6;  
  27.   
  28.     public final static Properties loadProperties(final String name, final int type) throws IOException {  
  29.         Properties p = new Properties();  
  30.         InputStream in = null;  
  31.         if (type == BY_PROPERTIES) {  
  32.             in = new BufferedInputStream(new FileInputStream(name));  
  33.             assert (in != null);  
  34.             p.load(in);  
  35.         } else if (type == BY_RESOURCEBUNDLE) {  
  36.             ResourceBundle rb = ResourceBundle.getBundle(name, Locale.ENGLISH);  
  37.             assert (rb != null);  
  38.             p = new ResourceBundleAdapter(rb);  
  39.         } else if (type == BY_PROPERTYRESOURCEBUNDLE) {  
  40.             in = new BufferedInputStream(new FileInputStream(name));  
  41.             assert (in != null);  
  42.             ResourceBundle rb = new PropertyResourceBundle(in);  
  43.             p = new ResourceBundleAdapter(rb);  
  44.         } else if (type == BY_CLASS) {  
  45.             assert (OperateProperties.class.equals(new OperateProperties().getClass()));  
  46.             in = OperateProperties.class.getResourceAsStream(name);  
  47.             assert (in != null);  
  48.             p.load(in);  
  49.             // return new JProperties().getClass().getResourceAsStream(name);  
  50.         } else if (type == BY_CLASSLOADER) {  
  51.             assert (OperateProperties.class.getClassLoader().equals(new OperateProperties().getClass().getClassLoader()));  
  52.             in = OperateProperties.class.getClassLoader().getResourceAsStream(name);  
  53.             assert (in != null);  
  54.             p.load(in);  
  55.             // return new JProperties().getClass().getClassLoader().getResourceAsStream(name);  
  56.         } else if (type == BY_SYSTEM_CLASSLOADER) {  
  57.             in = ClassLoader.getSystemResourceAsStream(name);  
  58.             assert (in != null);  
  59.             p.load(in);  
  60.         }  
  61.   
  62.         if (in != null) {  
  63.             in.close();  
  64.         }  
  65.         return p;  
  66.     }  
  67.       
  68.     public static class ResourceBundleAdapter extends Properties {  
  69.         /** 
  70.          *  
  71.          */  
  72.         private static final long serialVersionUID = 1L;  
  73.   
  74.         public ResourceBundleAdapter(ResourceBundle rb) {  
  75.             assert (rb instanceof java.util.PropertyResourceBundle);  
  76.             this.rb = rb;  
  77.             Enumeration<String> e = rb.getKeys();  
  78.             while (e.hasMoreElements()) {  
  79.                 Object o = e.nextElement();  
  80.                 this.put(o, rb.getObject((String) o));  
  81.             }  
  82.         }  
  83.   
  84.         private ResourceBundle rb = null;  
  85.   
  86.         public ResourceBundle getBundle(String baseName) {  
  87.             return ResourceBundle.getBundle(baseName);  
  88.         }  
  89.   
  90.         public ResourceBundle getBundle(String baseName, Locale locale) {  
  91.             return ResourceBundle.getBundle(baseName, locale);  
  92.         }  
  93.   
  94.         public ResourceBundle getBundle(String baseName, Locale locale, ClassLoader loader) {  
  95.             return ResourceBundle.getBundle(baseName, locale, loader);  
  96.         }  
  97.   
  98.         public Enumeration<?> getKeys() {  
  99.             return rb.getKeys();  
  100.         }  
  101.   
  102.         public Locale getLocale() {  
  103.             return rb.getLocale();  
  104.         }  
  105.   
  106.         public Object getObject(String key) {  
  107.             return rb.getObject(key);  
  108.         }  
  109.   
  110.         public String getString(String key) {  
  111.             return rb.getString(key);  
  112.         }  
  113.   
  114.         public String[] getStringArray(String key) {  
  115.             return rb.getStringArray(key);  
  116.         }  
  117.   
  118.         protected Object handleGetObject(String key) {  
  119.             return ((PropertyResourceBundle) rb).handleGetObject(key);  
  120.         }  
  121.     }  
  122. }  

PropertiesTest类:

[java] view plain copy
 
  1. package com.dengsilinming.read.properties;  
  2.   
  3. import java.util.Properties;  
  4.   
  5. import junit.framework.TestCase;  
  6.   
  7. /** 
  8.  * TODO Comment of PropertiesTest 
  9.  *  
  10.  * @author dengsilinming 
  11.  * @version $Id: PropertiesTest.java 2013-1-30 下午1:40:52 $ 
  12.  */  
  13. public class PropertiesTest extends TestCase {  
  14.       
  15.     OperateProperties jProperties;  
  16.     String      key   = "helloworld.title";  
  17.     String      value = "Hello World!";  
  18.   
  19.     public void testLoadProperties() throws Exception {  
  20.         String name = null;  
  21.         Properties p = new Properties();  
  22.   
  23.         name = "F:/workspace/DailyPractise/src/com/dengsilinming/read/properties/localeProperties.properties";  
  24.         p = OperateProperties.loadProperties(name, OperateProperties.BY_PROPERTIES);  
  25.         assertEquals(value, p.getProperty(key));  
  26.   
  27.         name = "com.dengsilinming.read.properties";  
  28.         p = OperateProperties.loadProperties(name, OperateProperties.BY_RESOURCEBUNDLE);  
  29.         assertEquals(value, p.getProperty(key));  
  30.         assertEquals(value, ((OperateProperties.ResourceBundleAdapter) p).getString(key));  
  31.   
  32.         name = "F:/workspace/DailyPractise/src/com/dengsilinming/read/properties/localeProperties.properties";  
  33.         p = OperateProperties.loadProperties(name, OperateProperties.BY_PROPERTYRESOURCEBUNDLE);  
  34.         assertEquals(value, p.getProperty(key));  
  35.         assertEquals(value, ((OperateProperties.ResourceBundleAdapter) p).getString(key));  
  36.   
  37.         name = "\\com\\dengsilinming\\read\\properties\\localeProperties.properties";  
  38.         p = OperateProperties.loadProperties(name, OperateProperties.BY_SYSTEM_CLASSLOADER);  
  39.         assertEquals(value, p.getProperty(key));  
  40.   
  41.         name = "\\com\\dengsilinming\\read\\properties\\localeProperties.properties";  
  42.         p = OperateProperties.loadProperties(name, OperateProperties.BY_CLASSLOADER);  
  43.         assertEquals(value, p.getProperty(key));  
  44.   
  45.         name = "\\properties\\localeProperties.properties";  
  46.         p = OperateProperties.loadProperties(name, OperateProperties.BY_CLASS);  
  47.         assertEquals(value, p.getProperty(key));  
  48.     }  
  49. }  

上面附上的代码在运行时是会报错的,大家自己去修改吧,都是一些很容易的问题,可不要偷懒哦吐舌头

需要说明的一点是:这是我在网上看到的一篇文章,自己稍稍的改动过一些代码;由于当时没太注意文章的出处,所以在此对原作者表示抱歉

posted @ 2016-11-02 16:12  William已上线  阅读(48)  评论(0)    收藏  举报