写入.properties
String path = ServletActionContext.getServletContext().getRealPath("");
Properties properties = new Properties();
try {
InputStream inputStream = new FileInputStream(path+"/config.properties");
properties.load(inputStream);
OutputStream outputStream = new FileOutputStream(path+"/config.properties");
properties.setProperty("database.url","jdbc:mysql://" + infomation.getDbUrl() + ":3306/"+systemInfomation.getDbName());
properties.setProperty("database.user",infomation.getDbUser());
properties.setProperty("database.password", infomation.getDbPassword());
properties.setProperty("administrator.username", infomation.getAdminName());
properties.setProperty("administrator.password", infomation.getAdminPassword());
properties.store(outputStream, "System Base Infomation(Database and Administrator)");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
读取.properties
private String readValue(String key) {
String path = ServletActionContext.getServletContext().getRealPath("");
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream(new FileInputStream(path+"/config.properties"));
props.load(in);
String value = props.getProperty(key);
return value;
} catch (Exception e) {
return null;
}
}