如何使用HashMap编写和读取文件?
我有一个带有两个字符串Map ldapContent = new HashMap
的HashMap
Map ldapContent = new HashMap
。
现在我想将Map
保存在外部文件中,以便稍后使用Map
而无需再次初始化它…
那么如何保存Map
以便以后再使用呢?
HashMap
实现Serializable
因此您可以使用常规序列化将hashmap写入文件
这是Java – Serialization示例的链接
我能想到的最简单的解决方案是使用Properties类。
保存地图:
Map ldapContent = new HashMap(); Properties properties = new Properties(); for (Map.Entry entry : ldapContent.entrySet()) { properties.put(entry.getKey(), entry.getValue()); } properties.store(new FileOutputStream("data.properties"), null);
加载地图:
Map ldapContent = new HashMap(); Properties properties = new Properties(); properties.load(new FileInputStream("data.properties")); for (String key : properties.stringPropertyNames()) { ldapContent.put(key, properties.get(key).toString()); }
编辑:
如果您的地图包含明文值,如果您通过任何文本编辑器打开文件数据,它们将是可见的,如果您序列化地图则不是这种情况:
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("data.ser")); out.writeObject(ldapContent); out.close();
EDIT2:
而不是for循环(由OldCurmudgeon建议)在保存示例中:
properties.putAll(ldapContent);
但是,对于加载示例,这是最好的:
ldapContent = new HashMap
由于HashMap
实现了Serializable
接口,你可以简单地使用ObjectOutputStream
类将整个Map
写入文件,并使用ObjectInputStream
类再次读取它
下面简单的代码解释了ObjectOutStream
和ObjectInputStream
用法
import java.util.*; import java.io.*; public class A{ HashMap hm; public A(){ hm=new HashMap(); hm.put("1","A"); hm.put("2","B"); hm.put("3","C"); method1(hm); } public void method1(HashMap map){ //write to file : "fileone" try{ File fileOne=new File("fileone"); FileOutputStream fos=new FileOutputStream(fileOne); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(map); oos.flush(); oos.close(); fos.close(); }catch(Exception e){} //read from file try{ File toRead=new File("fileone"); FileInputStream fis=new FileInputStream(toRead); ObjectInputStream ois=new ObjectInputStream(fis); HashMap mapInFile=(HashMap)ois.readObject(); ois.close(); fis.close(); //print All data in MAP for(Map.Entry m :mapInFile.entrySet()){ System.out.println(m.getKey()+" : "+m.getValue()); } }catch(Exception e){} } public static void main(String args[]){ new A(); } }
或者如果你想将数据作为文本写入文件,你可以简单地迭代Map
并逐行写入键和值,并逐行读取并添加到HashMap
import java.util.*; import java.io.*; public class A{ HashMap hm; public A(){ hm=new HashMap(); hm.put("1","A"); hm.put("2","B"); hm.put("3","C"); method2(hm); } public void method2(HashMap map){ //write to file : "fileone" try{ File fileTwo=new File("filetwo.txt"); FileOutputStream fos=new FileOutputStream(fileTwo); PrintWriter pw=new PrintWriter(fos); for(Map.Entry m :map.entrySet()){ pw.println(m.getKey()+"="+m.getValue()); } pw.flush(); pw.close(); fos.close(); }catch(Exception e){} //read from file try{ File toRead=new File("filetwo.txt"); FileInputStream fis=new FileInputStream(toRead); Scanner sc=new Scanner(fis); HashMap mapInFile=new HashMap(); //read data from file line by line: String currentLine; while(sc.hasNextLine()){ currentLine=sc.nextLine(); //now tokenize the currentLine: StringTokenizer st=new StringTokenizer(currentLine,"=",false); //put tokens ot currentLine in map mapInFile.put(st.nextToken(),st.nextToken()); } fis.close(); //print All data in MAP for(Map.Entry m :mapInFile.entrySet()){ System.out.println(m.getKey()+" : "+m.getValue()); } }catch(Exception e){} } public static void main(String args[]){ new A(); } }
注意:上面的代码可能不是执行此任务的最快方法,但我想显示一些类的应用程序
请参见ObjectOutputStream , ObjectInputStream , HashMap , Serializable , StringTokenizer
您可以使用ObjectOutputStream中的writeObject
将对象写入文件
的ObjectOutputStream