| 23794 |
govind |
1 |
package com.smartdukaan.cron.properties;
|
|
|
2 |
|
|
|
3 |
import java.io.File;
|
|
|
4 |
import java.io.FileInputStream;
|
|
|
5 |
import java.io.FileOutputStream;
|
|
|
6 |
import java.io.IOException;
|
|
|
7 |
import java.util.Iterator;
|
|
|
8 |
import java.util.Map;
|
|
|
9 |
import java.util.Map.Entry;
|
|
|
10 |
import java.util.Properties;
|
|
|
11 |
|
|
|
12 |
import org.apache.logging.log4j.LogManager;
|
|
|
13 |
import org.apache.logging.log4j.Logger;
|
|
|
14 |
import org.springframework.stereotype.Component;
|
|
|
15 |
|
|
|
16 |
@Component
|
|
|
17 |
public class WriteToPropertiesFile {
|
|
|
18 |
|
|
|
19 |
private static final Logger log = LogManager.getLogger(WriteToPropertiesFile.class);
|
|
|
20 |
|
|
|
21 |
public synchronized void saveParamChanges(Map<String, String> propertiesDetails,String propertiesFileLocation) throws IOException {
|
|
|
22 |
|
|
|
23 |
File propertiesFile=new File(propertiesFileLocation);
|
|
|
24 |
if (propertiesFile.exists()) {
|
|
|
25 |
Iterator<Entry<String, String>> it = propertiesDetails.entrySet().iterator();
|
|
|
26 |
while (it.hasNext()) {
|
|
|
27 |
Entry<String, String> pair = it.next();
|
|
|
28 |
try {
|
|
|
29 |
FileInputStream in = new FileInputStream(propertiesFile);
|
|
|
30 |
Properties props = new Properties();
|
|
|
31 |
props.load(in);
|
|
|
32 |
in.close();
|
|
|
33 |
FileOutputStream out = new FileOutputStream(propertiesFile);
|
|
|
34 |
props.setProperty(pair.getKey().toString(), pair.getValue().toString());
|
|
|
35 |
props.store(out, null);
|
|
|
36 |
out.close();
|
|
|
37 |
} catch (Exception e) {
|
|
|
38 |
log.info(e);
|
|
|
39 |
e.printStackTrace();
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
}
|
|
|
43 |
} else {
|
|
|
44 |
propertiesFile.createNewFile();
|
|
|
45 |
Iterator<Entry<String, String>> it = propertiesDetails.entrySet().iterator();
|
|
|
46 |
while (it.hasNext()) {
|
|
|
47 |
Entry<String, String> pair = it.next();
|
|
|
48 |
try {
|
|
|
49 |
FileOutputStream out = new FileOutputStream(propertiesFile);
|
|
|
50 |
Properties props = new Properties();
|
|
|
51 |
props.setProperty(pair.getKey().toString(), pair.getValue().toString());
|
|
|
52 |
props.store(out, null);
|
|
|
53 |
out.close();
|
|
|
54 |
} catch (Exception e) {
|
|
|
55 |
log.info(e);
|
|
|
56 |
e.printStackTrace();
|
|
|
57 |
}
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
}
|
|
|
61 |
}
|
|
|
62 |
}
|