Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将嵌套hashmap写入文件并删除Java中的元素时发生ConcurrentModificationException错误_Java - Fatal编程技术网

将嵌套hashmap写入文件并删除Java中的元素时发生ConcurrentModificationException错误

将嵌套hashmap写入文件并删除Java中的元素时发生ConcurrentModificationException错误,java,Java,我有一个代码,可以从一个文件中读取内容,然后在另一个文件中写入,如下所示。输入文件由调用日志组成,代码从文件中读取并为特定调用创建映射。某个特定的代理可以一次接听一个电话,并且一直待命,直到您到达包含挂断的线路。当代码到达该行时,我应该在文本文件中为该特定调用编写映射的内容,并刷新映射 inputfile.txt [DialerManager ] 15:40:35.688 MakeCall,CallId51,9972454323 [RandomId63 ] 15:40:57.562 Call

我有一个代码,可以从一个文件中读取内容,然后在另一个文件中写入,如下所示。输入文件由调用日志组成,代码从文件中读取并为特定调用创建映射。某个特定的代理可以一次接听一个电话,并且一直待命,直到您到达包含
挂断
的线路。当代码到达该行时,我应该在文本文件中为该特定调用编写映射的内容,并刷新映射

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
在这里,我想为每个调用创建一个映射,然后在到达
挂断时刷新映射。然后,接收器将进行新呼叫,从而为接收器创建新映射。我有下面的代码

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);
}
}
import java.io.*;
导入java.util.HashMap;
公共类ParseMapTest{
公共静态void main(字符串[]args)引发IOException{
缓冲读取器;
HashMap hMap=新的HashMap();
试一试{
reader=new BufferedReader(新文件读取器(“mapEx.txt”);
弦线;
而((line=reader.readLine())!=null){
if(line.contains(“MakeCall”)){
String[]words=line.split(“,|:|”);
for(int i=0;i

我正在创建一个文件out.txt,其中我正在编写已到达
挂起的内部映射,并将它们从
hMap
中删除,但是我得到了
java.util.ConcurrentModificationException
。如何纠正错误?提前感谢!

当您尝试从Ar中删除项目时,会出现此异常循环中的光线列表或Hashmap。为避免此类异常,请为要删除的项目创建单独的ArrayList,然后在单独的循环中删除这些项目。

这里有什么问题?不能使用数据库吗?要求是在文本文件中输出。我无法使用数据库。输出应如下所示:第一个带有
挂起的Hashmap,所以它是
{CallId51={Agent=Jimmy,AgentId=109,CallStatus=Connected}
。当它到达
挂起
时,它应该写入一个文本文件并刷新。然后到达
挂起
的下一个映射是针对
詹姆斯的。它应该写入同一个文件并刷新,依此类推。请参见:。在
hMap.keySet()上迭代时,不要执行
hMap.remove
。您只需在找到代理时存储包含该代理的标头,然后将其从循环外部删除即可。请参阅