-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTestJavaPropertiesFile.java
More file actions
61 lines (52 loc) · 1.46 KB
/
Copy pathTestJavaPropertiesFile.java
File metadata and controls
61 lines (52 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package testcase;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;
public class TestJavaPropertiesFile {
@SuppressWarnings("serial")
public static void main(String[] args) throws IOException {
String fileName = "C:\\Data\\New folder\\config.properties";
File propFile = null;
Properties aConfig = null;
FileOutputStream outPutStream = null;
try {
propFile = getPropertyFile(fileName);
HashMap<String, String> data = new HashMap<String,String>(){
{
put("KeyOne", "Value One");
put("KeyTwo", "Value Two");
put("KeyThree", "Value Three");
put("KeyFour", "Value Four");
}};
aConfig = insertProperty(data);
if(null != propFile){
outPutStream = new FileOutputStream(propFile);
aConfig.store(outPutStream, "Log4j Property File via code");
}
} finally {
if(outPutStream != null)
outPutStream.close();
}
}
private static Properties insertProperty(HashMap<String, String> data){
Properties aConfig = new Properties();
if(!(null == data || data.isEmpty())){
for (String key : data.keySet()) {
aConfig.setProperty(key, data.get(key));
}
}
return aConfig;
}
private static File getPropertyFile(String fileName) throws IOException{
File propFile = new File(fileName);
if(!propFile.exists())
propFile.createNewFile();
else{
propFile.delete();
propFile.createNewFile();
}
return propFile;
}
}