Reputation: 1077
I'm trying to save my config data in the config.properties
file. 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? I have no idea how to do it in another way.
Upvotes: 2
Views: 999
Reputation: 1780
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. 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.
Upvotes: 1
Reputation: 417672
First problem is that whenever you modify a property, you write the whole properties to the output
output stream. This is not how it was intended to work.
The Properties.store()
method will store all the properties stored in the Properties
object. So you should open your file right before calling the store()
method and close right after it.
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.
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.
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).
Upvotes: 1
Reputation: 10383
In loadExistingProperties()
you open both a FileInputStream
and a FileOutputStream
for the same file, but you only want to read from that file. Calling the FileOutputStream
will delete the contents of the file before you can read it. Simply remove that line.
Upvotes: 2
Reputation: 166
I have not seen your Properties
class but it seems to me that you did not close output stream nor did flush.
Does the file changes when you do prop.store(output, null);
?
Upvotes: 0