我基本上試圖將多個(12,特別是)HashMap字典寫入本地文件,然後檢索它們。到目前爲止,我設法做一個,但是當我嘗試做不止一個時,我基本上不能使它工作。所以任何幫助做到這一點表示讚賞。這裏是我到目前爲止的代碼:Java將多個HashMap寫入文件
private HashMap<String, List<String>> loadDict() {
int month = Functions.getMonth();
//load back in
FileInputStream fis;
try {
fis = new FileInputStream(statsFile);
ObjectInputStream ois = new ObjectInputStream(fis);
for (int i = 0; i < 13; i++) {
//itemsDict = (HashMap) ois.readObject();
Object whatisThis = (Object) ois.readObject();
dictionaries.add(whatisThis);
}
ois.close();
fis.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
itemsDict = (HashMap) dictionaries.get(month);
System.out.println(itemsDict.get("cake"));
return itemsDict;
}
private void setupDictionaries() {
HashMap<String, List<String>> dictionary = new HashMap<String,List<String>>();
FileOutputStream fos;
try {
fos = new FileOutputStream(statsFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
for (int i = 0; i < 13; i++) {
oos.writeObject(dictionary);
}
oos.close();
fos.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void storeThis(String product, String price, String quantity, String date, List<List<String>> myContent) {//after set, replace dictionary in dictionaries array
dictionaries.set(Functions.getMonth(), itemsDict);
//save the dictionary to the overall statistics file
FileOutputStream fos;
try {
fos = new FileOutputStream(statsFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
for (int i = 0; i < 13; i++) {
oos.writeObject(dictionaries.get(i));
}
//oos.writeObject(itemsDict);
oos.close();
fos.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
澄清一下:)setupDictionaries只調用在第一次運行(設置文件),否則loadDict(被稱爲上運行時,所有的字典加載到一個ArrayList 。從數組列表中,應該選擇正確的對象(hashmap),然後將其作爲散列表映射到itemsDict。 storeThis()在按下按鈕時被調用,但是我將代碼減少到只有相關的位。
所以我想實現你曾經建議JSON,到目前爲止我有:
private void setupDictionaries() {
ObjectMapper mapper = new ObjectMapper();
ArrayNode arrayNode = mapper.createArrayNode();
JsonNode rootNode = mapper.createObjectNode();
ArrayList<String> myThing = new ArrayList<String>();
myThing.add("hi");
myThing.add(".");
itemsDict.put("cake", myThing);
JsonNode childNode1 = mapper.valueToTree(itemsDict);
((ObjectNode) rootNode).set("Jan", childNode1);
JsonNode childNode2 = mapper.createObjectNode();
((ObjectNode) rootNode).set("obj2", childNode2);
JsonNode childNode3 = mapper.createObjectNode();
((ObjectNode) rootNode).set("obj3", childNode3);
String jsonString;
try {
jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(rootNode);
System.out.println(jsonString);
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
writer.writeValue(new File(statsFile), jsonString);
} catch (JsonProcessingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
問題,我怎麼就能夠加載該回來嗎? (僅一月下方一切例如,一個HashMap)
private HashMap<String, List<String>> loadDict() {
ObjectMapper mapper = new ObjectMapper();
try {
HashMap<String, ArrayList<String>> map = mapper.readValue(new File(statsFile), new TypeReference<HashMap<String, ArrayList<String>>>() {});
System.out.println(map.get("Jan"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
有了這個代碼,我將能夠加載它,但是我得到這個異常(因爲我有JSON的內的多個包含HashMap):
JsonMappingException:無法構建的java.util.HashMap的實例:沒有字符串參數的構造函數/工廠方法從字符串值反序列化
(我不知道如何把這裏的例外)
我的JSON:
「{\ r \ n \」Jan \「:{\ r \ n \」cake \「:[\」hi \「,\」。\「] \ r \ n} n「obj2」:{},\ r \ n \「obj3 \」:{} \ r \ n}「
那麼我怎樣才能夠只加載一個特定的月份到散列表?
你能詳細說明爲什麼你「不能使它工作」?究竟發生了什麼問題? – Siguza
爲什麼不只是導入/導出到JSON? – ControlAltDel
也許你正在寫13個HashMap字典而不是12個? – hmatar