I have a code to read the contents from a file and write in another file as follows. The input file consists of call logs and the code reads from the file and creates maps for a particular call. A particular agent can take one call at a time and is on call until you reach the line containing Hang up
. When the code reaches the line I should write the contents of the map for that particular call in text file and flush the map.
inputfile.txt
[DialerManager ] 15:40:35.688 MakeCall,CallId51,9972454323
[RandomId63 ] 15:40:57.562 CallId51 CallTransfer,Jimmy,109
[CallId51 ] 15:40:59.633 RandomId63 CALLSTATUS,CONNECTED
[TempId78 ] 15:51:27.586 Jimmy: Hang up
[DialerManager ] 15:40:35.688 MakeCall,CallId52,1234567890
[RandomId68 ] 15:40:57.562 CallId52 CallTransfer,James,100
[CallId52 ] 15:40:59.633 RandomId68 CALLSTATUS,CONNECTED
[TempId74 ] 15:51:27.586 James: Hang up
[DialerManager ] 15:40:35.688 MakeCall,CallId53,3456780912
[DialerManager ] 15:40:40.688 MakeCall,CallId54,9807652341
[RandomId01 ] 15:40:57.562 CallId53 CallTransfer,John,80
[CallId53 ] 15:40:59.633 RandomId01 CALLSTATUS,CONNECTED
[RandomId600 ] 15:40:57.562 CallId51 CallTransfer,Jimmy,109
[CallId54 ] 15:40:59.633 RandomId600 CALLSTATUS,CONNECTED
[TempId100 ] 15:53:27.586 John: Hang up
[TempId345 ] 15:56:27.586 Jimmy: Hang up
Here I want to create a map for each call and then flush the map when it reaches Hang up
. Then the receiver will proceed to a new call thereby creating a new map for the receiver. I have the code below.
code.java
import java.io.*;
import java.util.HashMap;
public class ParseMapTest {
public static void main(String[] args) throws IOException {
BufferedReader reader;
HashMap<String, HashMap<String, String>> hMap = new HashMap<>();
try {
reader = new BufferedReader(new FileReader("mapEx.txt"));
String line;
while ((line = reader.readLine()) != null) {
if (line.contains("MakeCall")) {
String[] words = line.split(",|:| ");
for (int i = 0; i < words.length; i++) {
if (words[i].startsWith("CallId")) {
String header = words[i];
if (!hMap.containsKey(header)) {
hMap.put(header, new HashMap<>());
}
}
}
} else if (line.contains("CallTransfer")) {
String header = line.split(" ")[4];
// System.out.println(header);
if (hMap.get(header) == null) {
hMap.put(header, new HashMap<>());
}
hMap.get(header).put("Agent", line.split(",")[1]);
hMap.get(header).put("AgentId", line.split(",")[2]);
} else if (line.contains("CALLSTATUS")) {
String headerBrack = line.split(" ")[0];
String header = headerBrack.replaceAll("[\\[]", "");
if (hMap.get(header) == null) {
hMap.put(header, new HashMap<>());
}
String[] lineSplit = line.split(",");
hMap.get(header).put("CALLSTATUS", lineSplit[lineSplit.length - 1]);
} else if (line.contains("Hang Up")) {
String agent = line.split("\\.|:")[3].substring(4);
for(String s: hMap.keySet()){
HashMap<String, String> switchMap = hMap.get(s);
if(switchMap.containsValue(agent)) {
System.out.println(s + hMap.get(s));
String map = s + hMap.get(s);
FileWriter fw = new FileWriter("out.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw);
out.println(map);
out.close();
hMap.remove(s);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(hMap);
}
}
I am creating a file out.txt where I am writing the inner maps which have reached Hang up
and deleting them from hMap
but I am getting java.util.ConcurrentModificationException
. How can I rectify the error? Thanks in advance!