在servlet中更改后自动重新加载属性文件

时间:2015-03-04 04:51:25

标签: java servlets

我正在使用servlet我从表单值更新属性文件值,但更改仅在我重新启动服务器时才会反映出来。请为我提供解决方案。

的Servlet

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    String database = request.getParameter("appDB");
     Properties prop = new Properties();
     String PROPERTY_FILENAME = "F:/Dashboard/Dashboard1.0/src/server_url.properties";
    FileInputStream file = new FileInputStream(PROPERTY_FILENAME);
    // load a properties file
    prop.load(file);

    OutputStream output = null;

    try {

        output = new FileOutputStream(PROPERTY_FILENAME);
        // set the properties value
        prop.setProperty("DemoApps_DataBase", database);
                    System.out.println(prop.getProperty("DemoApps_DataBase"));

        // save properties to project root folder
        prop.store(output, null);

    } catch (IOException io) {
        io.printStackTrace();
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}

1 个答案:

答案 0 :(得分:0)

让File对象指向属性文件并保留lastModifiedTime,每次比较lastModified与之前存储的时间,如果有更改重新读取文件。