【问题标题】:Create Properties file in Dynamic web project?在动态 Web 项目中创建属性文件?
【发布时间】:2013-04-22 08:52:31
【问题描述】:

大家好,我想在我的代码中动态创建一个属性文件

package abc.in;

import java.io.*;
import java.util.Properties;

public class DBConnection {
    OutputStream out = null;

    public void SetAllProps() {
        try {

            Properties props = new Properties();

            File f = new File("myApp.properties");
            if (f.exists()) {

                props.load(new FileReader(f));
                // Change your values here
                props.setProperty("ServerAddress", "ThatNewCoolValue");
            } else {

                // Set default values?
                props.setProperty("ServerAddress", "DullDefault");
                props.setProperty("ServerPort", "8080");
                props.setProperty("ThreadCount", "456");

                f.createNewFile();
            }

            out = new FileOutputStream(f);
            props.store(out, "This is an optional header comment string");

            System.out.println(props.get("ServerPort"));

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            if (out != null) {

                try {

                    out.close();
                } catch (IOException ex) {

                    System.out
                            .println("IOException: Could not close myApp.properties output stream; "
                                    + ex.getMessage());
                    ex.printStackTrace();
                }
            }
        }

    }
}

我在 java 项目中执行此操作,然后它会创建一个具有上述名称“myApp.properties”的属性文件,但是当我在动态 Web 项目中执行相同的代码时,它不会创建任何属性文件。我究竟做错了什么。期待您的回答。提前致谢

【问题讨论】:

    标签: jakarta-ee web-applications properties


    【解决方案1】:

    您可以通过f.getAbsolutePath() 获取您创建的属性文件的绝对路径。

    【讨论】:

      猜你喜欢
      • 2015-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-06
      相关资源
      最近更新 更多