在日常使用中經常會讀取properties檔案,現在把自己在網上搜集的一些java讀取properties的方法匯總如下:
1、使用java.util.Properties類的load()方法
如:
InputStream is = new BufferedInputStream(new FileInputStream(new File(properties檔案所在路徑));Properties properties = new Properties();properties.load(is);
2、使用java.util.ResourceBundle類的getBundle()方法
如:
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
3、使用java.util.PropertyResourceBundle類的建構函式
如:
InputStream is = new BufferedInputStream(new FileInputStream(name));ResourceBundle rb = new PropertyResourceBundle(is);
4、使用class變數的getResourceAsStream()方法
如:
InputStream in = 類名.class.getResourceAsStream(name);Properties p = new Properties();p.load(in);
5、使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
如:
InputStream in = 類名.class.getClassLoader().getResourceAsStream(name);Properties p = new Properties();p.load(in);
6、使用java.lang.ClassLoader類的getSystemResourceAsStream()靜態方法
如:
InputStream in = ClassLoader.getSystemResourceAsStream(name);Properties p = new Properties();p.load(in);
下面附上一些代碼
ResourceBundleAdapter類:
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 下午1:35:47 $ */public class ResourceBundleAdapter extends Properties { /** * */ private 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); } public ResourceBundle getBundle(String baseName, Locale locale) { return ResourceBundle.getBundle(baseName, locale); } public 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); } public String[] getStringArray(String key) { return rb.getStringArray(key); } protected Object handleGetObject(String key) { return ((PropertyResourceBundle) rb).handleGetObject(key); }}
OperateProperties類:
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 OperateProperties().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 { /** * */ private 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); } public ResourceBundle getBundle(String baseName, Locale locale) { return ResourceBundle.getBundle(baseName, locale); } public 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); } public String[] getStringArray(String key) { return rb.getStringArray(key); } protected Object handleGetObject(String key) { return ((PropertyResourceBundle) rb).handleGetObject(key); } }}
PropertiesTest類:
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 下午1:40:52 $ */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)); }}
上面附上的代碼在運行時是會報錯的,大家自己去修改吧,都是一些很容易的問題,可不要偷懶哦
需要說明的一點是:這是我在網上看到的一篇文章,自己稍稍的改動過一些代碼;由於當時沒太注意文章的出處,所以在此對原作者表示抱歉