Content of data.txt file
pin=9876
balance=9001
investment=10000
interest=0.065
isLockedOut=false
My code currently:
import java.io.*;
import java.util.Properties;
public class SetData extends ATM {
public static void setIsLockedOut(boolean isLockedOut) { //Sets the isLockedOut variable
try {
Properties data = new Properties();
FileOutputStream output = new FileOutputStream("data.txt");
if (isLockedOut = true) {
data.setProperty("isLockedOut", "true");
data.store(output, null);
output.close(); //Closes the output stream
}
else {
data.setProperty("isLockedOut", "false");
data.store(output, null);
output.close();
}
}
catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
I have also checked and referred to a similar question on StackOverflow as well ( Updating property value in properties file without deleting other values ).
The method 'setIsLockedOut' is called from another class. When I call this method to set the 'isLockedOut' variable to true in the 'data.txt' file, all other variables are erased except the 'isLockedOut' variable. This is the output:
#Sun Nov 17 15:44:42 EST 2013
isLockedOut=true
So my question is, how can I update a property value without erasing the other values in the file?
All you are doing is overwriting the data.txt file with the content of data
which is just the value of "isLockedOut". It seems that what you want to do is to overwrite data.txt with all of the properties that used to be in data.txt, plus an updated value for "isLockedOut". To do that, you need to open data.txt for reading and read its content into data
, then modify data
, then overwrite data.txt with the new data
. Skipping the first step is what is causing your problem.
You need to use a FileInputStream
and the load
method . Use them much the same way you are using FileOutputStream
and store
already.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.