使用java.util.Properties包可以轻松实现properties文件的读取
Main.java
public class Main {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
loadProperties(properties);
Object test = properties.getProperty("test");
System.out.println("test!:" + test);
}
private static void loadProperties(Properties p) throws IOException {
InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("Config.properties");
p.load(inputStream);
}
}
Config.properties 配置文件
test = 1234567
结果
主要步骤:
- 实例化一个Properties类
- properties.load(InputStream input) 载入配置文件
- properties.getProperty(String key) 进行配置的读取