Java 如何更新属性文件?

Java 如何更新属性文件?,java,properties,Java,Properties,我正在使用classLoader方法加载属性文件。使用这个,我可以检索属性的值,但是现在我想更新一些属性的值,但我无法这样做。请帮忙。以下是代码: InputStream InputStream=this.getClass.getClassLoader .getResourceAsStreamPublishDate.properties try { Properties properties = new Properties(); try { // load th

我正在使用classLoader方法加载属性文件。使用这个,我可以检索属性的值,但是现在我想更新一些属性的值,但我无法这样做。请帮忙。以下是代码:

InputStream InputStream=this.getClass.getClassLoader .getResourceAsStreamPublishDate.properties

try {
    Properties properties = new Properties();

    try {
       // load the inputStream using the Properties
       properties.load(inputStream);
    }
    catch(Exception e) {
       e.printStackTrace();
    }
    // get the value of the property
    String propValue = properties.getProperty("lastHtlProcessPublishDate");
这里,在propValue中,我从文件中获得正确的值。我正在这样更新:

properties.put("lastHtlProcessPublishDate",dateFormatter.format(new Date()));   
properties.store(new FileOutputStream("PublishDate.properties"), null);
使用这个值不会得到更新,但是当我提供PublishDate.properties的完整路径时,它就可以工作了。 但是,我不想给出完整的路径,因为路径是动态的。是否有人知道如何使用某个相对路径执行此操作。请就此向我提供建议。

用于处理您的属性文件。它将使读取和写入任何配置文件的属性文件…和您的生活更轻松

编辑 下载commons配置二进制文件 如果您的项目有classpathlib目录,请将其放在classpathlib目录中的某个位置 运行一个干净的项目构建,这样Eclipse我假设可以选择新的类 为您的操作导入所需的类并编写代码:D 用于处理属性文件。它将使读取和写入任何配置文件的属性文件…和您的生活更轻松

编辑 下载commons配置二进制文件 如果您的项目有classpathlib目录,请将其放在classpathlib目录中的某个位置 运行一个干净的项目构建,这样Eclipse我假设可以选择新的类 为您的操作导入所需的类并编写代码:D
您可能希望尝试使用PropertiesConfiguration。这应该可以满足您的所有需求。

您可能需要尝试使用PropertiesConfiguration。这应该可以满足您的所有需求。

您好,我更新了代码:PropertiesConfiguration config=new PropertiesConfiguration PublishDate.properties;config.setPropertylastHtlProcessPublishDate,dateFormatter.formatnew Date;config.save;但我在控制台上看到了这一点:线程线程1 java.lang.NoClassDefFoundError中的异常:org/apache/commons/configuration/PropertiesConfiguration位于com.galileo.localdata.servlets.MVSDataServlet.runMVSDataServlet.java:176位于java.lang.thread.runThread.java:619,原因是:java.lang.ClassNotFoundException:org.apache.commons.configuration.PropertiesConfiguration您是否导入了PropertiesConfiguration类?这就是那个错误的意思。是的,我导入了这个类,并且为这个类放置了JAR。然后运行一个干净的项目构建,这样Eclipse我假设可以获取新的类,但仍然得到相同的错误。之前我得到的类型是org.apache.commons.lang.exception.NestableException无法解析。它是从必需的.class文件间接引用的。所以,为了解决这个问题,我添加了commons-lang-exception-2.0.jar来构建路径。发生此错误是因为此原因吗?您好,我更新了代码:PropertiesConfiguration config=new PropertiesConfiguration PublishDate.properties;config.setPropertylastHtlProcessPublishDate,dateFormatter.formatnew Date;config.save;但我在控制台上看到了这一点:线程线程1 java.lang.NoClassDefFoundError中的异常:org/apache/commons/configuration/PropertiesConfiguration位于com.galileo.localdata.servlets.MVSDataServlet.runMVSDataServlet.java:176位于java.lang.thread.runThread.java:619,原因是:java.lang.ClassNotFoundException:org.apache.commons.configuration.PropertiesConfiguration您是否导入了PropertiesConfiguration类?这就是那个错误的意思。是的,我导入了这个类,并且为这个类放置了JAR。然后运行一个干净的项目构建,这样Eclipse我假设可以获取新的类,但仍然得到相同的错误。之前我得到的类型是org.apache.commons.lang.exception.NestableException无法解析。它是从必需的.class文件间接引用的。所以,为了解决这个问题,我添加了commons-lang-exception-2.0.jar来构建路径。发生此错误是因为此原因吗?final AbstractFileConfiguration fileConfiguration=新属性配置PublishDate.properties;fileConfiguration.setProperty lastHtlProcessPublishDate,dateFormatter.format新日期;fileConfiguration.save;final AbstractFileConfiguration fileConfiguration=新属性配置PublishDate.properties;fileConfiguration.setProperty lastHtlProcessPublishDate,dateFormatter.format新日期;fileConfiguration.save;