1、先在项目中创建一个包(如:config),再创建一个配置文件(如:a.properties),添加配置信息如下:
比如:
name=kaka
age=28
2、代码:
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertyTest {
public static void main(String[] args) {
PropertyTest loadProp = new PropertyTest();
InputStream in = loadProp.getClass().getResourceAsStream("/config/a.properties");
Properties prop = new Properties();
try {
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(prop.getProperty("name"));
System.out.println(prop.getProperty("age"));
}
}