java读取properties配置文件

2021-05-12 05:28

阅读:449

标签:目录   NPU   trace   com   http   name   java读取   author   utils   

目录结构

技术图片

 

package com.wish.config;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * @Author: lfyu
 * @DateTime: 2020-06-15 10:37
 * @ProjectName: loginVerify
 * @PackageName: com.wish.config
 * @ClassName: ReadConfig
 * @Description:
 **/
public class ReadConfig {

    public static Map map = new HashMap();
/*    public static Map read(){
        Map map = new HashMap();
        Properties properties = new Properties();
        try {
            properties = PropertiesLoaderUtils.loadAllProperties("config.properties");
            String port = properties.getProperty("redis.port");
            String host = properties.getProperty("redis.host");
            String timeout = properties.getProperty("redis.timeout");
            String use = properties.getProperty("loginFilter.use");
            map.put("port", port);
            map.put("host", host);
            map.put("timeout", timeout);
            map.put("use", use);
            return map;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }*/

    public static Map read(){
        Properties properties = new Properties();

        try {
            InputStream in = ReadConfig.class.getClassLoader().getResourceAsStream("config.properties");
            properties.load(in);
            Iterator iterator = properties.stringPropertyNames().iterator();
            while (iterator.hasNext()) {
                String key = iterator.next();
                String value = properties.getProperty(key);
                map.put(key,value);
//                System.out.println("key = " + key + ",value = " +value);
            }
            return map;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}

config.properties文件内容

技术图片

 

 

  

执行结果

技术图片

 

java读取properties配置文件

标签:目录   NPU   trace   com   http   name   java读取   author   utils   

原文地址:https://www.cnblogs.com/lfyu/p/13141801.html


评论


亲,登录后才可以留言!