zoukankan      html  css  js  c++  java
  • java读取properties配置文件

    目录结构

    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文件内容

      

    执行结果

  • 相关阅读:
    如何快速正确的安装 Ruby, Rails 运行环境
    五种开源协议的比较(BSD,Apache,GPL,LGPL,MIT) – 整理
    CocoaPods安装和使用教程
    ARC下需要注意的内存管理
    iOS 遍历某一对象的属性和方法
    使用命令行工具运行Xcode 7 UI Tests
    手势知多少
    Customizing UIWebView requests with NSURLProtocol
    iOS: JS和Native交互的两种方法
    NSURLProtocol
  • 原文地址:https://www.cnblogs.com/lfyu/p/13141801.html
Copyright © 2011-2022 走看看