无法找到Spring java -.properties文件

时间:2016-05-05 20:00:20

标签: java spring maven properties-file applicationcontext

我尝试更新“ex.properties”文件中的数据。 applicationContext.xml文件使用当前数据。我尝试了在互联网上找到的每个解决方案,但当“CurrencyAddJob.java”试图访问和更改数据时,我仍然无法确定“ex.properties”文件的路径。

“ex.properties”文件applicationContext.xml的相关部分:

<bean id="dailyCountJobCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    <property name="jobDetail" ref="dailyCountJob"/>
    <property name="group" value="DailyJobsTriggers"/>
    <property name="cronExpression" value="${sched1}"/>
</bean>

<bean id="schedProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:ex.properties" />
</bean>

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="environment">
        <bean class="org.springframework.web.context.support.StandardServletEnvironment"/>
    </property>
</bean>

“ex.properties”文件:

sched1=0/4 * * * * ?

“ex.properties”的位置:src / main / resources / ex.properties

CurrencyAddJob.java: 解决方案1:

    try {
            FileInputStream in = new FileInputStream("ex.properties");
            Properties props = new Properties();

            props.load(in);
            in.close();
            FileOutputStream out = new FileOutputStream("ex.properties");

            props.setProperty("sched1", "0/1 * * * * ?");
            props.store(out, null);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

发现了一个异常:

  

java.io.FileNotFoundException:ex.properties(系统无法找到   指定的文件)

CurrencyAddJob.java: 解决方案2:

    try {
            FileInputStream in = new FileInputStream("C:\Users\Honorius\Desktop\workspace\honoriusProject\src\main\resources\ex.properties");
            Properties props = new Properties();

            props.load(in);
            in.close();
            FileOutputStream out = new FileOutputStream("C:\Users\Honorius\Desktop\workspace\honoriusProject\src\main\resources\ex.properties");

            props.setProperty("sched1", "0/1 * * * * ?");
            props.store(out, null);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

发现了一个异常:

  

java.io.FileNotFoundException:ex.properties(系统无法找到   指定的文件)

CurrencyAddJob.java: 解决方案3:

    try {
            FileInputStream in = new FileInputStream(new File("ex.properties").getAbsolutePath());
            Properties props = new Properties();

            props.load(in);
            in.close();
            FileOutputStream out = new FileOutputStream(new File("ex.properties").getAbsolutePath());

            props.setProperty("sched1", "0/1 * * * * ?");
            props.store(out, null);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

发现了一个异常:

  

java.io.FileNotFoundException:ex.properties(系统无法找到   指定的文件)

我还更改了“ex.properties”的目录,但我无法摆脱异常

有没有替代解决方案? 任何建议将不胜感激。

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

    try {
        FileInputStream in = new FileInputStream(this.getClass().getResourceAsStream("/ex.properties"));
        Properties props = new Properties();

        props.load(in);
        in.close();
        FileOutputStream out = new FileOutputStream(new File(this.getClass().getResource("/ex.properties").getFile()));

        props.setProperty("sched1", "0/1 * * * * ?");
        props.store(out, null);
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }