养成写博客的好习惯-第一次写博客

工作中,有些偏基础,偏需要用脑子记的东西,到关键时刻总是想不起来,还不如写个博客记录下来,还正好练习了写博客,最好是能养成习惯就好了。

今天先写个Java读取配置文件的吧。

需求:读取resources下的配置文件
工具:Intellij IDEA
jdk版本:1.8
好,开始!
--------------------------------------------------------
新建个maven工程,就叫blogs,引入junit包,新建个module,叫blogs-properties吧,用来写读取配置文件的代码。resources目录下新建两个properties配置文件,jdbc.properties;redis.properties,里面分别写点配置,就来读取这俩配置文件的内容。


jdbc.properties

jdbc.url=jdbc:mysql://localhost:3306/consultation?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true
jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=root

redis.properties

redis.host=127.0.0.1
redis.port=6379
redis.master=master

思路:通过InputStream读取文件内容,用Properties.load()来加载文件内容,写个工具类
PropertyUtil.java

package com.river.properties.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Properties;

/**
 * 配置文件工具类
 * @author River
 * @date 2018/5/29
 */
public class PropertyUtil {

    private static Properties properties = new Properties();

    static {
        loadProperties();
    }

    /**
     * 根据key获取配置信息
     * @param key
     * @return
     */
    public static String getProperty(String key) {
        return properties.getProperty(key);
    }

    /**
     * 获取所有配置信息
     * @return
     */
    public static Properties getAllProperties() {
        return properties;
    }

    private static void loadProperties() {
        File files = new File("src/main/resources/properties");
        if (files.isDirectory()) {
            Arrays.asList(files).forEach(file -> loadFileProperties(file));
        } else {
            System.out.println("没加载进来");
        }
    }

    private static void loadFileProperties(File file) {
        try {
            FileInputStream inputStream = new FileInputStream(file);
            properties.load(inputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private PropertyUtil() {

    }
}

工具类写完了,我们写个单元测试来测试一下

package com.river.properties.util;

import org.junit.Assert;
import org.junit.Test;
import java.util.Properties;

/**
 * 配置文件工具类单元测试类
 * @author River
 * @date 2018/5/29
 */
public class PropertyUtilTest {

    private static final String KEY = "jdbc.driver";
    private static final String VALUE = "com.mysql.jdbc.Driver";

    @Test
    public void testGetAllProperties() {
        Properties allProperties = PropertyUtil.getAllProperties();
        Assert.assertTrue(allProperties.size() > 0);
        allProperties.forEach((key, value) -> System.out.println(key + ":" + value));
    }

    @Test
    public void testGetProperty() {
        String driver = PropertyUtil.getProperty(KEY);
        Assert.assertEquals(driver, VALUE);
        System.out.println(KEY + ":" + driver);
    }
}
testGetAllProperties方法测试结果:

testGetProperty方法测试结果:

测试完成。
-------------------------------------------------------------------------------------
第一篇博客就先这样了,接下来还是针对读取配置文件的再写点,这种方法不实用,有一些问题。
后面这加个链接,链接到第二篇博客上。

猜你喜欢

转载自blog.csdn.net/Rongfeiyue/article/details/80498812