I am using following code for writing data to properties file
public void WritePropertiesFile(String key, String data)
{
Properties configProperty = new Properties();
configProperty.setProperty(key, data);
File file = new File("D:\\Helper.properties");
FileOutputStream fileOut = new FileOutputStream(file,true);
configProperty.store(fileOut, "sample properties");
fileOut.close();
}
I am calling the above method 3 times as follows:
help.WritePropertiesFile("appwrite1","write1");
help.WritePropertiesFile("appwrite2","write2");
help.WritePropertiesFile("appwrite3","write3");
However, the data in Helper.properties file is displayed as follows:
#sample properties
#Mon Jul 01 15:01:45 IST 2013
appwrite1=write1
#sample properties
#Mon Jul 01 15:01:45 IST 2013
appwrite2=write2
appwrite1=write1
#sample properties
#Mon Jul 01 15:01:45 IST 2013
appwrite3=write3
appwrite2=write2
appwrite1=write1
I want data to append to existing data and doesn't want duplicate data, that is as follows:
appwrite3=write3
appwrite2=write2
appwrite1=write1
Please suggest how to do it?
Example 2: Append text to an existing file using FileWriterWhen creating a FileWriter object, we pass the path of the file and true as the second parameter. true means we allow the file to be appended. Then, we use write() method to append the given text and close the filewriter.
Load the updated properties file into the data grid by entering one of the following commands in the install_dir \Members\bin directory: execute config load all (for update of more than one properties file) execute config load datasource (for update of data sources properties file)
Just do not open the file in append mode.
You read existing properties from the file and the write them again. If you append to the file, all the contents of the Properties
object will be appended since this is what you asked for.
Just replace:
FileOutputStream fileOut = new FileOutputStream(file,true);
with:
FileOutputStream fileOut = new FileOutputStream(file);
Side note: you should .close()
your output stream in a finally
block.
I know this has been answered, but just for future reference code should look more-less like this:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
class WritePropertiesFile {
public void WritePropertiesFile(String key, String data) {
FileOutputStream fileOut = null;
FileInputStream fileIn = null;
try {
Properties configProperty = new Properties();
File file = new File("D:\\Helper.properties");
fileIn = new FileInputStream(file);
configProperty.load(fileIn);
configProperty.setProperty(key, data);
fileOut = new FileOutputStream(file);
configProperty.store(fileOut, "sample properties");
} catch (Exception ex) {
Logger.getLogger(WritePropertiesFile.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fileOut.close();
} catch (IOException ex) {
Logger.getLogger(WritePropertiesFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void main(String[] args) {
WritePropertiesFile help = new WritePropertiesFile();
help.WritePropertiesFile("appwrite1", "write1");
help.WritePropertiesFile("appwrite2", "write2");
help.WritePropertiesFile("appwrite3", "write3");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With