在日常使用中经常会读取properties文件,现在把自己在网上搜集的一些java读取properties的方法汇总如下:
1、使用java.util.Properties类的load()方法
如:
如:
如:
如:
如:
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));
}
}
上面附上的代码在运行时是会报错的,大家自己去修改吧,都是一些很容易的问题,可不要偷懒哦
需要说明的一点是:这是我在网上看到的一篇文章,自己稍稍的改动过一些代码;由于当时没太注意文章的出处,所以在此对原作者表示抱歉