1、先在resource目录下创建配置文件 config.properties
内容格式为:key=value ,key可以是单个词,也可以多词以点符号(.) 连接,如果value是windows文件路径,单斜杠(\)需要换为双斜杠(\)eg:
2、在读取类的构造函数中读取
public class ReadConfig {
public static String url;
public static String userName;
public static String password;
public static String path;
public ReadConfig(){
Properties properties = new Properties();
InputStream input = null;
try {
input = getClass().getClassLoader().getResourceAsStream("config.properties");
assert input != null;
properties.load(new InputStreamReader(input, "UTF-8"));
url = properties.getProperty("datasource.url");
userName = properties.getProperty("datasource.username");
password = properties.getProperty("datasource.password");
path = properties.getProperty("path");
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(input!=null){
input.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}