简体   繁体   English

重新启动程序后从属性文件读取

[英]Read from property file after restarting program

I'm trying to save my config data in the config.properties file. 我正在尝试将配置数据保存在config.properties文件中。 I'm saving my properties like this: 我这样保存我的属性:

public static void setProperty(Parameter property) {

    // set the property's value
    prop.setProperty(property.getIndex(), property.getValue());

    // save properties to project root folder
    try {
        prop.store(output, null);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

and load them like this: 并像这样加载它们:

public static String getProperty(String componentName) {

    // get the property value
    return prop.getProperty(componentName);
}

and everything works fine, but when I restart my program, I don't have any properties anymore. 并且一切正常,但是当我重新启动程序时,我没有任何属性了。 I call following method in the beginning of my program for loading my property file: 我在程序的开头调用以下方法来加载我的属性文件:

static String FILE = "config.properties";
static InputStream input;
static OutputStream output;
static Properties prop = new Properties();

public static void loadExistingProperties() {
    try {
        input = new FileInputStream(FILE);
        output = new FileOutputStream(FILE);

        // load properties from file
        prop.load(input);
        System.out.println("amount of saved properties: " + prop.size());

    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Can anyone tell me why I can't find my properties anymore after restarting the program? 有人可以告诉我为什么重启程序后找不到属性吗? Is the way I'm trying to get my prop wrong maybe? 是我尝试弄错prop的方式吗? I have no idea how to do it in another way. 我不知道该怎么做。

In loadExistingProperties() you open both a FileInputStream and a FileOutputStream for the same file, but you only want to read from that file. loadExistingProperties()您为同一文件同时打开FileInputStreamFileOutputStream ,但是您只想从该文件读取。 Calling the FileOutputStream will delete the contents of the file before you can read it. 调用FileOutputStream将删除文件的内容,然后才能读取它。 Simply remove that line. 只需删除该行。

I believe when you open your outputstream it truncates the content of the file. 我相信,当您打开输出流时,它会截断文件的内容。

EDITED: 编辑:

You need to open your input stream, read the contents of the property file without opening the output stream. 您需要打开输入流,在不打开输出流的情况下读取属性文件的内容。

If you do need to save them later, you can still open you output stream in your setProperty method and call the store method. 如果以后确实需要保存它们,则仍然可以在setProperty方法中打开输出流并调用store方法。 Or you can open your output stream after having read all the properties and closed the input stream. 或者,您可以在阅读所有属性并关闭输入流之后打开输出流。

Keep in mind that the store method will save again all the properties. 请记住,store方法将再次保存所有属性。

First problem is that whenever you modify a property, you write the whole properties to the output output stream. 第一个问题是,每当修改属性时,都会将整个属性写入output输出流。 This is not how it was intended to work. 这不是预期的工作方式。

The Properties.store() method will store all the properties stored in the Properties object. Properties.store()方法将存储存储在Properties对象中的所有Properties So you should open your file right before calling the store() method and close right after it. 因此,您应该在调用store()方法之前立即打开文件,并在紧随其后关闭文件。

It looks to me that you never close output which might result in data written to it still existing only in in-memory cache and never written to the underlying file. 在我看来,您永远不会关闭output ,这可能会导致写入数据的数据仍然仅存在于内存缓存中,并且永远不会写入基础文件。

Simply persist properties like this: 只需保留如下属性:

try (OutputStream out = new FileOutputStream("config.properties")) {
    prop.store(out, null);
}

And load them simply like this: 像这样简单地加载它们:

try (InputStream in = new FileInputStream("config.properties")) {
    prop.load(in);
}

The try-with-resources blocks will properly close the streams. try-with-resources块将正确关闭流。

Also you should not persist properties every time you modify one. 同样,您不应该在每次修改属性时都保留属性。 Common thing is to only save properties/settings when you close the program or when the user asks you to do it (like a "Save settings now" menu). 常见的事情是仅在关闭程序或用户要求您执行操作时才保存属性/设置(例如“立即保存设置”菜单)。

I have not seen your Properties class but it seems to me that you did not close output stream nor did flush. 我没有看到您的Properties类,但是在我看来,您没有关闭输出流也没有刷新。 Does the file changes when you do prop.store(output, null); 在执行prop.store(output, null);时文件是否更改prop.store(output, null); ?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM