项目代码中加载 配置文件

 1 import java.io.IOException;
 2 import java.io.InputStream;
 3 import java.util.Properties;
 4 
 5 public class T3 {
 6     public static void main(String[] args) throws IOException {
 7         getProperties();
 8     }
 9 
10     private static void getProperties() throws IOException {
11         //加载项目根目录(或者maven项目的resource目录)下的config.properties文件为流
12         InputStream inputStream = T3.class.getClassLoader().getResourceAsStream("config.properties");
13         //创建p对象
14         Properties properties = new Properties();
15         //p对象加载流
16         properties.load(inputStream);
17         inputStream.close();
18         System.out.println("name:" + properties.getProperty("name"));
19         System.out.println("id:" + properties.getProperty("id"));
20     }
21 }
22  

config.properties文件内容:(字符串不用加引号)

1 name=haha;
2 id = 123;
原文地址:https://www.cnblogs.com/libin6505/p/7717013.html