文章目录
- 代码
- 演示效果
- 参考文档
代码
package com.test.ljj;import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.PropertyResourceBundle;
import java.util.Set;public class ReadProperty01 {// static String url = "D:\\set.properties.txt";public static void TimingProperties() throws Exception {//url 改成你的.properties配置文件的路径,作者这里用的是绝对路径,最好是用相对路径String url="C:\\Program Files\\Study\\IntelliJ IDEA 2022.3\\work_space\\测试\\Leetcode\\code.properties";File file = new File(url);InputStream inputStream = new FileInputStream(file);PropertyResourceBundle p = new PropertyResourceBundle(inputStream);try {Set<String> key = p.keySet();Map<String, Object> map = new HashMap<>();for (String getkey : key) {map.put(getkey, p.getString(getkey));}System.out.println(map);} catch (Exception e) {}finally {System.out.println();}}public static void main(String[] args) throws Exception {TimingProperties();}}
演示效果
参考文档
- Java读取properties文件的六种方法