Java工具类--读取Properties文件

雨点打透心脏的1/2处 2022-08-19 01:45 418阅读 0赞

读取配置文件已经成了Java程序员工作的一项必备技能。
配置文件的优点:

可维护性好

怎么个可维护性好呢?

  1. 它会让程序中变化的地方很灵活的配置,不需要修改代码。
  2. Java程序部署到服务器上去之后就变成了class文件,修改困难,通过配置文件我们就可以灵活地改变程序中需要变化的地方。比如说写一个发送邮件的程序,就可以将收件人写在配置文件中,不必每次编译代码。
  3. 假如过了很久,项目经理觉得有些地方需要改,以发邮件为例,现在需要给大boss也发邮件,而自己手头又没有代码,通过配置文件只需要修改配置文件即可。

废话不多说,直接进入主题,如何通过Java读取配置文件,其实很简单。

配置文件

  1. name=刘德华
  2. pwd=123456

代码如下

  1. package com.test.util;
  2. import java.io.InputStreamReader;
  3. import java.util.Enumeration;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.Properties;
  7. /** * 读取配置文件的工具类 * * @author 程高伟 * @date 2016年12月27日 下午9:08:23 */
  8. public class PropertiesUtil {
  9. private Properties props;
  10. public PropertiesUtil(String fileName) {
  11. readProperties(fileName);
  12. }
  13. /** * 加载配置文件 * * @param fileName */
  14. private void readProperties(String fileName) {
  15. try {
  16. props = new Properties();
  17. InputStreamReader inputStream = new InputStreamReader(
  18. this.getClass().getClassLoader().getResourceAsStream(fileName), "UTF-8");
  19. props.load(inputStream);
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. /** * 根据key读取对应的value * * @param key * @return */
  25. public String get(String key) {
  26. return props.getProperty(key);
  27. }
  28. /** * 得到所有的配置信息 * * @return */
  29. public Map<?, ?> getAll() {
  30. Map<String, String> map = new HashMap<String, String>();
  31. Enumeration<?> enu = props.propertyNames();
  32. while (enu.hasMoreElements()) {
  33. String key = (String) enu.nextElement();
  34. String value = props.getProperty(key);
  35. map.put(key, value);
  36. }
  37. return map;
  38. }
  39. }

测试

city.properties

  1. 乌鲁木齐=Urumqi
  2. package com.test.util;
  3. import org.junit.Test;
  4. public class PropertiesUtilTest {
  5. @Test
  6. public void testGet() {
  7. PropertiesUtil prop = new PropertiesUtil("city.properties");
  8. System.out.println(prop.get("乌鲁木齐"));
  9. }
  10. }

这里写图片描述

只有一个配置文件的写法

  1. import java.io.InputStreamReader;
  2. import java.util.Enumeration;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Properties;
  6. /** * 读取配置文件的工具类 * * @author 程高伟 * @date 2016年12月27日 下午9:08:23 */
  7. public class PropertiesUtil {
  8. private static Properties props;
  9. // public PropertiesUtil(String fileName) {
  10. // readProperties(fileName);
  11. // }
  12. static{
  13. readProperties("config.properties");
  14. }
  15. /** * 加载配置文件 * * @param fileName */
  16. private static void readProperties(String fileName) {
  17. try {
  18. props = new Properties();
  19. InputStreamReader inputStream = new InputStreamReader(
  20. PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName), "UTF-8");
  21. props.load(inputStream);
  22. } catch (Exception e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. /** * 根据key读取对应的value * * @param key * @return */
  27. public static String get(String key) {
  28. return props.getProperty(key);
  29. }
  30. /** * 得到所有的配置信息 * * @return */
  31. public static Map<?, ?> getAll() {
  32. Map<String, String> map = new HashMap<String, String>();
  33. Enumeration<?> enu = props.propertyNames();
  34. while (enu.hasMoreElements()) {
  35. String key = (String) enu.nextElement();
  36. String value = props.getProperty(key);
  37. map.put(key, value);
  38. }
  39. return map;
  40. }
  41. }

为什么没有设置值的方法呢?

因为配置文件一般都是只读的,需要设置的话,已经部署到服务器上去了,停掉服务,然后修改配置文件,就可以了。

读取配置文件的工具类先写到这里。

修复了中文乱码的错误

参考文献

Java读取properties文件中文乱码问题解决

发表评论

表情:
评论列表 (有 0 条评论,418人围观)

还没有评论,来说两句吧...

相关阅读