Java工具类--读取Properties文件
读取配置文件已经成了Java程序员工作的一项必备技能。
配置文件的优点:
可维护性好
怎么个可维护性好呢?
- 它会让程序中变化的地方很灵活的配置,不需要修改代码。
- Java程序部署到服务器上去之后就变成了class文件,修改困难,通过配置文件我们就可以灵活地改变程序中需要变化的地方。比如说写一个发送邮件的程序,就可以将收件人写在配置文件中,不必每次编译代码。
- 假如过了很久,项目经理觉得有些地方需要改,以发邮件为例,现在需要给大boss也发邮件,而自己手头又没有代码,通过配置文件只需要修改配置文件即可。
废话不多说,直接进入主题,如何通过Java读取配置文件,其实很简单。
配置文件
name=刘德华
pwd=123456
代码如下
package com.test.util;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/** * 读取配置文件的工具类 * * @author 程高伟 * @date 2016年12月27日 下午9:08:23 */
public class PropertiesUtil {
private Properties props;
public PropertiesUtil(String fileName) {
readProperties(fileName);
}
/** * 加载配置文件 * * @param fileName */
private void readProperties(String fileName) {
try {
props = new Properties();
InputStreamReader inputStream = new InputStreamReader(
this.getClass().getClassLoader().getResourceAsStream(fileName), "UTF-8");
props.load(inputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
/** * 根据key读取对应的value * * @param key * @return */
public String get(String key) {
return props.getProperty(key);
}
/** * 得到所有的配置信息 * * @return */
public Map<?, ?> getAll() {
Map<String, String> map = new HashMap<String, String>();
Enumeration<?> enu = props.propertyNames();
while (enu.hasMoreElements()) {
String key = (String) enu.nextElement();
String value = props.getProperty(key);
map.put(key, value);
}
return map;
}
}
测试
city.properties
乌鲁木齐=Urumqi
package com.test.util;
import org.junit.Test;
public class PropertiesUtilTest {
@Test
public void testGet() {
PropertiesUtil prop = new PropertiesUtil("city.properties");
System.out.println(prop.get("乌鲁木齐"));
}
}
只有一个配置文件的写法
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/** * 读取配置文件的工具类 * * @author 程高伟 * @date 2016年12月27日 下午9:08:23 */
public class PropertiesUtil {
private static Properties props;
// public PropertiesUtil(String fileName) {
// readProperties(fileName);
// }
static{
readProperties("config.properties");
}
/** * 加载配置文件 * * @param fileName */
private static void readProperties(String fileName) {
try {
props = new Properties();
InputStreamReader inputStream = new InputStreamReader(
PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName), "UTF-8");
props.load(inputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
/** * 根据key读取对应的value * * @param key * @return */
public static String get(String key) {
return props.getProperty(key);
}
/** * 得到所有的配置信息 * * @return */
public static Map<?, ?> getAll() {
Map<String, String> map = new HashMap<String, String>();
Enumeration<?> enu = props.propertyNames();
while (enu.hasMoreElements()) {
String key = (String) enu.nextElement();
String value = props.getProperty(key);
map.put(key, value);
}
return map;
}
}
为什么没有设置值的方法呢?
因为配置文件一般都是只读的,需要设置的话,已经部署到服务器上去了,停掉服务,然后修改配置文件,就可以了。
读取配置文件的工具类先写到这里。
修复了中文乱码的错误
参考文献
Java读取properties文件中文乱码问题解决
还没有评论,来说两句吧...