首页 > 解决方案 > 将 Long 文件扫描成 java map

问题描述

我正在尝试读取我使用另一个代码创建的文本文件来填写地图,但是扫描仪继续提前停止,但是如果我删除了文本文档中停止的行,扫描仪将在获取之前继续运行一段时间又卡住了。我不明白为什么它会卡在某些线路上。我添加了一个指向我正在扫描的文本文档的链接。例如,无论何时编写恶魔城线的第一个实例都会阻塞系统,在文档中它会显示在第 307 行。如果删除该行,它会在第 383 行的 Conta 停止崩溃。然后如果是删除它在第 479 行的 Dig Dug 处中断。它继续以这种方式停止在名称或行号上似乎没有关系的随机行上。 文本文档下载

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
public class Read {
public static  PrintWriter writer;
public static Map<String, LinkedHashMap<String, String>> connectionsMap = new LinkedHashMap<String, LinkedHashMap<String,String>>() ;

public static void main(String[] args) {
    setUp();
    Iterator it = connectionsMap.entrySet().iterator();
    int i =0;
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry)it.next();
        System.out.println(pair.getKey() +" "+i);
        i++;
        it.remove(); // avoids a ConcurrentModificationException
    }
}
public static void setUp() {
    File file = new File("Degrees.txt");//file with all the connections
    try {
        Scanner scanner = new Scanner(file);
        String description = "";
        String key="";//key for map of main series
        String secondKey="";//key for map of a mian serieses connections
        String text = "";
        while(scanner.hasNext()) {
            String see = scanner.next();
            if(see.equals("TITLEEND")) {
                key=text;
                text="";
            }
            if(see.equals("CONNECTIONTITLEEND")) {
                secondKey=text;
                text="";
            }
            if(see.equals("DESCRIPTIONEND")||see.equals("TERMINATE")) {
                description =text;
                text="";
                mapMaker(key,secondKey,description, false);

            }if(!see.equals("TERMINATE")&&!see.equals("CONNECTIONTITLEEND")&&!see.equals("DESCRIPTIONEND")&&!see.equals("TITLEEND")) {
                if(text.length()<1)text=see;
                else text+=" "+see;
            }

        }   
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
public static void mapMaker(String key, String secondKey, String description, boolean recursed) { 
    if(!connectionsMap.containsKey(key)) {
        connectionsMap.put(key, new LinkedHashMap<String,String>());
    }if(!connectionsMap.get(key).containsKey(secondKey)) {
        connectionsMap.get(key).put(secondKey, description);
    }

if(!recursed) {
    mapMaker(secondKey,key,description,true);
}

}

标签: javatextjava.util.scanner

解决方案


推荐阅读