• properties配置文件读取


    1.配置文件test.properties:

    test_123=admin

    注:value 可用单引号,双引号,不用引号修饰

    2.工具类PropertiesUtil:

    package com.......test;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    
    public class PropertiesUtil {

    private static final String PROP_FILE = "test.properties";

    private static Properties properties;

    static {
    properties = new Properties();
    try {
    InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(PROP_FILE);
    BufferedReader bf = new BufferedReader(new InputStreamReader(is, "UTF-8"));//
    解决读取properties文件中产生中文乱码的问题
    assert (is != null);
    properties.load(bf);

    } catch (IOException e) {
    throw new RuntimeException("读取" + PROP_FILE + "配置文件异常", e);
    }
    }

    public static String getValue(String key) {
    return properties.getProperty(key);
    }
    }
    
    

    3.测试

        @Test
        public void test() {
            String  keyWord = "test_123";
            String value = PropertiesUtil.getValue(keyWord);
            System.out.println("value = " + value);
        }
  • 相关阅读:
    Redis Java客户端之Lettuce
    Redis Java客户端之Redisson
    Redis Java客户端的比较
    Redis布隆过滤器
    过期删除策略和内存淘汰策略
    集群模式详解
    哨兵模式详解
    Redis Java客户端之Jedis
    主从复制
    AOF持久化
  • 原文地址:https://www.cnblogs.com/zt007/p/8571579.html
Copyright © 2020-2023  润新知