0

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

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 项目中执行相同的代码时,它不会创建任何属性文件。我究竟做错了什么。期待您的回答。提前致谢

4

1 回答 1

0

您可以执行 af.getAbsolutePath()来获取您创建的属性文件的绝对路径。

于 2013-04-22T13:33:46.717 回答