写入和读取.properties文件

写入.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;
     }
  }

 

 

猜你喜欢

转载自caiaihuan.iteye.com/blog/1895081
今日推荐