java读取自定义的.properties 配置文件 中的key-value值

  /**
     * 读取 .properties 配置文件
     * @param propertiesUrl 配置文件的路径
     * @return 配置文件中的key-value值
     */
    public static Map<String, String> getProperties(String propertiesUrl) {
        Map<String, String> resultMap = new HashMap<String, String>();
        Properties prop = new Properties();
        try {
            // 读取属性文件a.properties
            InputStream in = new BufferedInputStream(new FileInputStream(propertiesUrl));
            // 加载属性列表
            prop.load(in);
            Iterator<String> it = prop.stringPropertyNames().iterator();
            while (it.hasNext()) {
                String key = it.next();
                resultMap.put(key, prop.getProperty(key));
                System.out.println("key: "+key+",      value: "+prop.getProperty(key));
            }
            in.close();
        } catch (Exception e) {
            System.out.println(e);
        }
        return resultMap;
    }

原文地址:https://www.cnblogs.com/52KT9/p/12527038.html

时间: 2024-07-30 17:28:44

java读取自定义的.properties 配置文件 中的key-value值的相关文章

Java 读取application.properties配置文件中配置

实际开发中若需要读取配置文件application.properties中的配置,代码如下.例:读取配置文件中name属性配置值: 代码如下: import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PropertiesLoaderUtils; import java.u

Java 将自定义的对象作为HashMap的key

需要继承Map的equals函数和hashCode函数 package com.category; import java.util.HashMap; public class GenCategoryLevelData { private static HashMap<Category, Integer> categoryLevel = new HashMap<Category, Integer>(); /** * @param args */ public static void

java 读取src下的配置文件

很多时候,我们都将配置文件放在eclipse的src目录下,这个位置,相当于,当导出可执行jar包后,配置文件放在和jar同级的目录中,比如jar包放在/opt目录下,则配置文件放在/opt下,则jar包就可以读取配置文件中的内容.此时,java代码中可以通过ClassName.class.getResourceAsStream("/ConfigFileName")(返回一个InputStream流对象);来读取配置文件中的内容,ClassName换成具体类名,ConfigFileNa

java读取资源文件(Properties)

四步: java代码 //new一个读取配置文件 Properties properties=new Properties(); //获取文件路径 String path=request.getServletContext().getRealPath("/dom.properties"); //读取文件内容 properties.load(new FileInputStream(path)); //获取origin的value String origin=properties.getP

.properties配置文件中的常量在其他文件中的引用

一.在其他xml配置文件中引用 在maven项目中scr/main/resource 中新建properties文件夹存放项目中各种环境下的配置文件 在配置文件中以键值对形式写好后,在spring配置文件中将配置文件初始化成Bean,让后就可以在其他xml文件中已${.....}形式引用 <!-- 初始化properties配置文件成bean--> <bean  id="configProperties" class="org.springframework

java自定义类型 作为HashMap中的Key值 (Pair&lt;V,K&gt;为例)

由于是自定义类型,所以HashMap中的equals()函数和hashCode()函数都需要自定义覆盖. 不然内容相同的对象对应的hashCode会不同,无法发挥算法的正常功能,覆盖equals函数,应该就相当于c++重载==运算符来保证能判断是否相等.只不过java没有自定义重载运算符这个功能的,需要进行函数覆盖. equals的函数原型是 boolean equals(Object o);注意括号内.hashCode的函数原型就是int hashCode(); 先看一段代码: import

java读取txt文件的2中方法---并将内容(每一行以固定的字符分割切成2段)存到map中去

#java读取txt文件的第一种方法 /** * 方法:readTxt * 功能:读取txt文件并把txt文件的内容---每一行作为一个字符串加入到List中去 * 参数:txt文件的地址 * 返回:Map * @param file * @return * @throws IOException */ public static Map<String, String> readTxt(String file) throws IOException { Map<String, Strin

Java用自定义的类型作为HashMap的key

??需要重写hashCode()和equals()方法才可以实现自定义键在HashMap中的查找. public class PhoneNumber { private int prefix; //区号 private int phoneNumber; //电话号 public PhoneNumber(int prefix, int phoneNumber) { this.prefix = prefix; this.phoneNumber = phoneNumber; } } import ja

Java读取自定义环境变量的方法System.getProperty和System.getenv

引自:https://blog.csdn.net/zhen8023wan/article/details/38460389 问题的由来,想在测试机环境下搞测试,然后不想修改搞到线上环境的时候,自动把本地测试的代码去掉,第一反应的是使用ip判断,但是某些时候也许没有request参数,那么我们变通一下,只需要在测试环境上搞一个自定义的环境变量,然后用来作为判断条件即可.假设设置了环境变量MYNAME, 值为”this is my name”, 运行如下代码: System.out.println(