Subversion Repositories SmartDukaan

Rev

View as "text/plain" | Blame | Last modification | View Log | RSS feed

package com.smartdukaan.cron.properties;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

@Component
public class WriteToPropertiesFile {

        private static final Logger log = LogManager.getLogger(WriteToPropertiesFile.class);

        public synchronized void saveParamChanges(Map<String, String> propertiesDetails,String propertiesFileLocation) throws IOException {

                File propertiesFile=new File(propertiesFileLocation);
                if (propertiesFile.exists()) {
                        Iterator<Entry<String, String>> it = propertiesDetails.entrySet().iterator();
                        while (it.hasNext()) {
                                Entry<String, String> pair = it.next();
                                try {
                                        FileInputStream in = new FileInputStream(propertiesFile);
                                        Properties props = new Properties();
                                        props.load(in);
                                        in.close();
                                        FileOutputStream out = new FileOutputStream(propertiesFile);
                                        props.setProperty(pair.getKey().toString(), pair.getValue().toString());
                                        props.store(out, null);
                                        out.close();
                                } catch (Exception e) {
                                        log.info(e);
                                        e.printStackTrace();
                                }

                        }
                } else {
                        propertiesFile.createNewFile();
                        Iterator<Entry<String, String>> it = propertiesDetails.entrySet().iterator();
                        while (it.hasNext()) {
                                Entry<String, String> pair = it.next();
                                try {
                                        FileOutputStream out = new FileOutputStream(propertiesFile);
                                        Properties props = new Properties();
                                        props.setProperty(pair.getKey().toString(), pair.getValue().toString());
                                        props.store(out, null);
                                        out.close();
                                } catch (Exception e) {
                                        log.info(e);
                                        e.printStackTrace();
                                }
                        }

                }
        }
}