java读取properties文件的方法

1:使用apache.commons组件。

需要使用jar包:commons-configuration-1.3.jar,commons-lang-2.3.jar,commons-collections.3.2.jar,commons-logging.jar

public class ConfigReader {

private static ConfigReader instance =new ConfigReader();
public static Configuration config ;
private ConfigReader(){
try{
config = new PropertiesConfiguration("test.properties");
}catch(NestableRuntimeException e2){
System.out.println("properties read error...");
}catch(ConfigurationException e3){
System.out.println("properties read error...");
}
}

public static ConfigReader getInstance(){
return instance ;
}

public static void main(String args[]) throws RemoteException, NestableRuntimeException, ConfigurationException {
System.out.println(ConfigReader.getInstance().config.getProperty("userid"));
}
}

2:使用java.util.ResourceBundle(国际化使用的类)的getBundle()方法读取properties文件来获取值

public class ResourceBundleReader {
public final static Object initLock = new Object();

private final static String PROPERTIES_FILE_NAME = "property";

private static ResourceBundle bundle = null;

static {
try {
if (bundle == null) {
synchronized (initLock) {
if (bundle == null)
bundle = ResourceBundle.getBundle(PROPERTIES_FILE_NAME,Locale.CHINA);
}
}
} catch (Exception e) {
System.out.println("读取资源文件property_zh.properties失败!");
}
}
public static ResourceBundle getBundle() {
return bundle;
}
public static void setBundle(ResourceBundle bundle) {
bundle = bundle;
}


}
3:springMVC的话


<!-- 应用属性文件读入 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer -->
<bean id="applicationProperties" class="com.kuke.core.util.PropertiesHolder">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:/jdbc.properties</value>
<value>classpath:/redis.properties</value>
<value>classpath:/application.properties</value>
</list>
</property>
</bean>



package com.kuke.core.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

public class PropertiesHolder extends PropertyPlaceholderConfigurer {

private static Map<String, Object> ctxPropertiesMap;

@Override
protected void processProperties(
ConfigurableListableBeanFactory beanFactoryToProcess,
Properties props) throws BeansException {
super.processProperties(beanFactoryToProcess, props);
ctxPropertiesMap = new HashMap<String, Object>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
ctxPropertiesMap.put(keyStr, value);
}
}

public static Object getContextProperty(String name) {
return ctxPropertiesMap.get(name);
}
}



4:利用java.util.Properties

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ipConfig.properties");
Properties p = new Properties();
try {
p.load(inputStream);
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("ip:"+p.getProperty("ip")+",port:"+p.getProperty("port"));



5:使用java.util.PropertyResourceBundle类的构造函数
示例: InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);


6:使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);

7:使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
示例: InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new Properties();
p.load(in);

补充

Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值