-1
public class readBin {
public static void main(String[] args) throws IOException{
    long time1 = System.currentTimeMillis();


File targetfile = new File("d:\\d2012.bin");
    FileInputStream in = new FileInputStream(targetfile);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    byte[] buffer = new byte[2097152];
    int byteread = 0;
    String bufferString = "";

    ArrayList<HashMap> arr = new ArrayList<HashMap>();
    ArrayList arrstr = new ArrayList();
    HashMap hashmap = new HashMap();
    ArrayList<String> cttarr = new ArrayList<String>();
    String[] strarr = new String[2];
    ArrayList<String> valarr = new ArrayList<>();
    //It's all init above,below comes the file reading
    bufferString = br.readLine();
    while(bufferString != null){
        //if data readed this line from the file is not "*newrecord" or ""
        if(!bufferString.equals("*NEWRECORD")&&!bufferString.equals("")){
            //according to the need,make use of these data
            strarr = bufferString.split("=");
            switch (strarr[0].trim()) {
            case "UI":
                hashmap.put(strarr[0].trim(), strarr[1].trim());
                break;
            case "MH":
                hashmap.put(strarr[0].trim(), strarr[1].trim());
                break;
            case "AQ":
                String[] valuearr = strarr[1].split(" ");
                hashmap.put(strarr[0], valuearr);
                break;
            case "ENTRY":
                bufferString = bufferString.split("\\|")[0].toString();
                //if key named ENTRY has already existed,update the data
                if (hashmap.containsKey(strarr[0].trim())) {
                    ArrayList<String> templist = ((ArrayList<String>)hashmap.get(strarr[0].trim()));
                    templist.add(bufferString.split("=")[1].trim());
                    hashmap.put(strarr[0].trim(), templist);
                //or insert it 
                } else {
                    cttarr.add(bufferString);
                    hashmap.put(strarr[0].trim(),cttarr);
                }
                break;
            case "MS":
                hashmap.put(strarr[0].trim(), strarr[1].trim());
                break;
            case "MN":
                //as ENTRY do
                if (hashmap.containsKey(strarr[0].trim())) {
                    ArrayList<String> templist =     ((ArrayList<String>)hashmap.get(strarr[0].trim()));
                    templist.add(strarr[1].trim());
                    hashmap.put(strarr[0].trim(), templist);
                } else {
                    cttarr.add(strarr[1].trim());
                    hashmap.put(strarr[0].trim(),cttarr);
                }
                break;

            default:
                break;
            }
        } else if(hashmap.size() != 0) {
            //if it equals to *newrecord or "",init the hashmap again
            arr.add(hashmap);
            hashmap = new HashMap();
        }
        bufferString = br.readLine();
        if (bufferString == null) {
            arr.add(hashmap);
        }
    }
    ArrayList arrresult = arr;
    long time2 = System.currentTimeMillis();
    System.out.println(time2-time1);
    String ui = (String) arr.get(0).get("MH");
    ArrayList<String> entrys = (ArrayList<String>) arr.get(0).get("ENTRY");
    int len = arr.size();
    System.out.println(ui);
    System.out.println(len);
}

public String getbs(){
    return "";
}
}

我想从文件中迭代数据并将它们添加到hashmap中,在将hashmap更新为arrayList之后,再次初始化hashmap。但是从结果来看,hashmap中的数据似乎不会清除。

4

2 回答 2

0

很明显,因为不知道 new HashMap() 会保留前一个实例的数据

    } else if(hashmap.size() != 0) {
        //if it equals to *newrecord or "",init the hashmap again
        arr.add(hashmap);
        hashmap = new HashMap();
    }

当你希望它被执行时不被执行。所以检查输入文件的格式,和/或尝试调试。

于 2012-07-04T09:55:54.343 回答
0

OK,现在我知道学好英语有多重要了。

我现在明白了,在集合中,它总是引用而不是实体,所以我不应该这样写代码

ArrayList<String> templist = ((ArrayList<String>)hashmap.get(strarr[0].trim()));

当我打开它时ArrayList<String> templist = new ArrayList<String>((ArrayList<String>)hashmap.get(strarr[0].trim()));,它运行良好。

于 2012-07-05T05:19:26.560 回答