java 读取properties配置文件代码,使用java中的Properties类

介绍几个Properties类里面的常用函数:

它提供了几个主要的方法:

1. getProperty ( String key),用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。

2. load ( InputStream inStream),从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。

3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。

4. store ( OutputStream out, String comments),以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。

5. clear (),清除所有装载的 键 - 值对。该方法在基类中提供。

对了Properties类是继承HashMaptable的,可能与HashMap和HashTable所存的键和值不能为空有关

假设配置文件,config.properties。在类加载路径下,文件中的key,value格式如下:

jdbc.url=jdbc:mysql://10.129.156.29:3306/test?useUnicode=true&characterEncoding=utf8
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.username=test
jdbc.password=123456
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.springframework.core.io.ClassPathResource;
import com.thinry.commons.utils.log.Log;


public class PropertiesUtils {
    

    /**
     * read properties file and return Map
     * @param fileName
     * @param wantKeyToValue
     * @return
     */
    public static Map<String, String> readPropertiesAndReturnMap(String fileName, boolean wantKeyToValue) {
        Map<String, String> mapRet1 = null;
        try {
            Properties props = new Properties();
            ClassPathResource classPathResource = new ClassPathResource(fileName);//去类加载路径下加载配置文件。
            InputStream in = classPathResource.getInputStream();
            InputStreamReader inputStream = new InputStreamReader(in, "UTF-8");
            props.load(inputStream);
            mapRet1 = returnMap(props, wantKeyToValue);
        } catch (IOException e) {
            Log.error("PropertiesUtils readPropertiesAndReturnMap Exception", e);
        } catch (Exception e) {
            Log.error("PropertiesUtils readPropertiesAndReturnMap Exception", e);
        }
        return Collections.unmodifiableMap(mapRet1);//  返回不可改变的map,数据装载后就不需要改变了,改变这个map中的数据会报UnsupportedOperationException。
    }


    /**
     * return Map for properties files
     * @param propsTemp
     * @param wantKeyToValue
     * @return
     */
    private static Map<String, String> returnMap(Properties propsTemp, boolean wantKeyToValue) {
        Map<String, String> map = new HashMap<String, String>();
        Enumeration<?> enu = propsTemp.propertyNames();
        while (enu.hasMoreElements()) {
            String key = (String) enu.nextElement();
            String value = propsTemp.getProperty(key);
            if (wantKeyToValue) {
                map.put(key, value);
            } else {
                map.put(value, key);
            }
        }
        return map;
    }
}

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Java可以通过Properties读取properties配置文件。具体步骤如下: 1. 创建Properties对象 Properties prop = new Properties(); 2. 加载配置文件 prop.load(new FileInputStream("config.properties")); 3. 读取配置文件的属性值 String value = prop.getProperty("key"); 其,key为配置文件的属性名,value为属性值。 需要注意的是,配置文件的路径需要根据实际情况进行修改,可以使用相对路径或绝对路径。另外,如果配置文件存在文字符,需要使用UTF-8编码格式进行读取。 ### 回答2: Java读取properties配置文件的方法很简单,可以借助JavaProperties实现。 首先,需要引用java.util.Properties,通过load()方法读取配置文件数据,返回一个Properties对象。其配置文件可以是任何文件型,但其结构必须按照.properties格式书写。在读取时,可以使用相对或绝对路径指定配置文件的位置。 读取配置文件的示例代码如下: ``` import java.util.*; import java.io.*; public class ReadProperties { public static void main(String[] args) { Properties props = new Properties(); try { FileInputStream in = new FileInputStream("config.properties"); props.load(in); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); } String url = props.getProperty("url"); String user = props.getProperty("user"); String password = props.getProperty("password"); System.out.println("url: " + url); System.out.println("user: " + user); System.out.println("password: " + password); } } ``` 上述代码,首先创建了一个Properties对象,并通过调用load()方法读取配置文件config.properties数据。然后,通过调用getProperty()方法分别获取了配置文件的url、user和password属性值,并将其输出到控制台。 另外,也可以使用Class.getResourceAsStream()或ClassLoader.getResourceAsStream()方法读取配置文件。示例代码如下: ``` import java.util.*; import java.io.*; public class ReadProperties { public static void main(String[] args) { Properties props = new Properties(); try { InputStream in = ReadProperties.class.getResourceAsStream("/config.properties"); props.load(in); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); } String url = props.getProperty("url"); String user = props.getProperty("user"); String password = props.getProperty("password"); System.out.println("url: " + url); System.out.println("user: " + user); System.out.println("password: " + password); } } ``` 参考上述示例代码读取properties配置文件非常简单,只需要使用JavaProperties和相关方法即可。值得注意的是,在读取配置文件时,需要注意配置文件的位置及格式,以避免出现读取失败或读取错误的情况。 ### 回答3: properties 配置文件是一种常见的文本格式文件,其结构似键值对。在 Java 读取 properties 配置文件非常方便,在 Java 的标准库已经提供了相关的 API。本文将介绍 Java 读取 properties 配置文件的方法。 Java 提供了 java.util.Properties 用于操作 properties 文件。该实现了 Map 接口,可以通过 key-value 的方式存储和操作属性,在 Java 读取 properties 文件通常需要进行两个步骤: 1.创建 Properties 对象,同时将 properties 文件的内容加载到 Properties 对象。 2.通过 Properties 对象获取流配置值。 读取 Properties 文件,首先需要创建一个 Properties 对象,通常使用 Properties 的 load 方法来加载 properties 文件,load 方法支持两种方法加载 properties 文件。其一是使用文件路径加载,其二是使用 InputStream 对象加载。 public void load(InputStream inStream) public void load(Reader reader) 对于第一种方式,看一下下面的代码Properties properties = new Properties(); properties.load(new FileInputStream("/path/to/my.properties")); 对于第二种方式,将 InputStream 对象传递给 Properties 对象,调用 load 方法来读取内容: InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("my.properties"); Properties properties = new Properties(); properties.load(inputStream); 完成 Properties 文件的加载后,接下来就可以通过 getProperty() 方法获取到 key 对应的 value 内容了: String value = properties.getProperty("key"); 当然,Properties 对象还提供了很多其他的方法用于操作 properties 文件,例如,store() 方法用于存储 properties 文件, toString() 方法可用于将 Properties 对象转化为字符串等等。 总的来说,Java 读取 properties 配置文件非常简单,通过上述方法,可以轻松得到 properties 文件配置项,完成自己关于 properties 文件的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值