Java 未更新类路径中的属性

Java 未更新类路径中的属性,java,Java,我的项目下的资源文件夹中有一个属性文件。我可以加载该文件,但当我尝试写入时,它不会得到更新。我相信这个问题被问了好几次。如果有人给我一个解决方案,我将非常感激 Servlet protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method s

我的项目下的资源文件夹中有一个属性文件。我可以加载该文件,但当我尝试写入时,它不会得到更新。我相信这个问题被问了好几次。如果有人给我一个解决方案,我将非常感激

Servlet

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

    String name = request.getParameter("appName");
    String link = request.getParameter("appLink");
    String database = request.getParameter("appDB");
    String webServices = request.getParameter("appWebService");
    System.out.println(name);

    Properties prop = new Properties();
    String propFileName = "server_url.properties";

    InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(propFileName);
    prop.load(inputStream);
    System.out.println(propFileName);
    if (name.equalsIgnoreCase(prop.getProperty("DemoApps_name"))) {
        prop.setProperty("DemoApps_DataBase", database);
        prop.setProperty("DemoApps_Links", link);
        prop.setProperty("DemoApps_WebServices", webServices);
        System.out.println(prop.getProperty("DemoApps_DataBase"));
    }
    prop.store(new FileWriter(propFileName),"Update App details" );     System.out.println(propFileName +" "+"written successfully");


    response.sendRedirect("updateappstatus.jsp");

}

首先,您没有写入原始文件所在的位置。 从类路径读取原始文件,同时写入当前工作目录

其次,您不应该能够写入应用程序的类路径。如果您在web应用程序中提供属性文件,则无法对此进行修改(我不知道Servlet规范的细节,但可能有合同禁止您这样做,或者容器实现者甚至可以将web应用程序的类保留在打包的jar/war文件中,因此类和资源是只读的


您可以做的是发送一个默认属性文件,将其读取为备用,并将可写属性文件保存在容器外的某个位置,如此问题的答案中所述:

我遇到了相同的问题。web项目将有两个不同的运行位置。在eclipse上运行该项目时,将加载该项目到
eclipseworkspace
目录

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {   
    String currentPath = getServletContext().getRealPath("/") + "/WEB-INF/classes/com/server_url.properties";
    String name = request.getParameter("name");
    System.out.println(name);
    try {
        System.out.println(getPropertyValue("name",currentPath));
        setPropertyValue("name", name,currentPath);
        System.out.println(getPropertyValue("name",currentPath));
    } catch (Exception e) {
        e.printStackTrace();
    }
}


public static void setPropertyValue(String key, String value,String path) throws Exception {
    java.io.InputStream inputStream = null;
    FileOutputStream outputProperty = null;
    try {
        Properties projectProperties = new Properties();
        inputStream = new FileInputStream( path);
        projectProperties.load(inputStream);
        outputProperty = new FileOutputStream( path);
        projectProperties.setProperty(key, value);
        projectProperties.store(outputProperty, null);
    } catch (Exception e) {
        throw e;
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
        if (outputProperty != null) {
            outputProperty.close();
        }
    }
}

public static String getPropertyValue(String key,String path) throws Exception {
    java.io.InputStream inputStream = null;
    try {
        Properties projectProperties = new Properties();
        inputStream = new FileInputStream(path);
        projectProperties.load(inputStream);
        return projectProperties.getProperty(key);
    } catch (Exception e) {
        throw e;
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
    }
}

这些代码将用于获取或设置属性值。另一种最好的方法是,在web应用程序之外拥有属性文件。

new FileWriter(propFileName)
是否确定!您正在写入相同的文件位置!!!as
propFileName=“server\u url.properties”
No..当我试图获取绝对路径时,它显示了一个不同的路径-
C:\Desktop\eclipse\server\u url.properties
如果不指定绝对路径,相对路径取决于启动应用程序的方式。如果从eclipse启动,则从配置的bin文件夹加载类路径资源(在更改时会被覆盖)。选中此选项我不想指定绝对路径,因为它在生产服务器中部署时不起作用。