码迷,mamicode.com
首页 > 编程语言 > 详细

java读取properties配置文件

时间:2020-06-16 16:55:57      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:目录   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<String,String> map = new HashMap<>();
/*    public static Map<String,Object> read(){
        Map<String,Object> 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<String,String> read(){
        Properties properties = new Properties();

        try {
            InputStream in = ReadConfig.class.getClassLoader().getResourceAsStream("config.properties");
            properties.load(in);
            Iterator<String> 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!