将LinkedHashMap写入文本文件?
这个问题在这里已经有了答案:
因为你正试图将整个Map写入文件而不是单个条目,所以你可以像这样使用writeObject()
和readObject()
:
Map<String, String> m1 = new LinkedHashMap<String, String>(16, 0.75f, true);
m1.put("John Smith", "555-555-5555");
m1.put("Jane Smith", "444-444-4444");
//Write to file
FileOutputStream fout = new FileOutputStream("file.out");
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(m1);
//Read from file
FileInputStream fin = new FileInputStream("file.out");
ObjectInputStream ois = new ObjectInputStream(fin);
Map<String, String> m2 = (LinkedHashMap<String, String>) ois.readObject();
希望这可以帮助。
链接地址: http://www.djcxy.com/p/17987.html上一篇: Write LinkedHashMap to a text file?
下一篇: best way to call a method of each object in a collection