如何使用HashMap写入和读取文件?
2022-09-02 11:05:55
我有一个带两个字符串。HashMap
Map<String, String> ldapContent = new HashMap<String, String>
现在我想保存在外部文件中以使用以后的文件,而无需再次初始化它...Map
Map
那么我必须如何保存才能在以后再次使用它呢?Map
我有一个带两个字符串。HashMap
Map<String, String> ldapContent = new HashMap<String, String>
现在我想保存在外部文件中以使用以后的文件,而无需再次初始化它...Map
Map
那么我必须如何保存才能在以后再次使用它呢?Map
我能想到的最简单的解决方案是使用属性类。
保存地图:
Map<String, String> ldapContent = new HashMap<String, String>();
Properties properties = new Properties();
for (Map.Entry<String,String> entry : ldapContent.entrySet()) {
properties.put(entry.getKey(), entry.getValue());
}
properties.store(new FileOutputStream("data.properties"), null);
加载地图:
Map<String, String> ldapContent = new HashMap<String, String>();
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();
编辑2:
而不是for循环(如OldCurmudgeon所建议的那样)在保存示例中:
properties.putAll(ldapContent);
但是,对于加载示例,这是可以做到的最好的:
ldapContent = new HashMap<Object, Object>(properties);
由于实现了接口,您可以简单地使用class将整个写入文件,然后使用class再次读取它HashMap
Serializable
ObjectOutputStream
Map
ObjectInputStream
下面的简单代码,解释 和 的用法ObjectOutStream
ObjectInputStream
import java.util.*;
import java.io.*;
public class A{
HashMap<String,String> hm;
public A() {
hm=new HashMap<String,String>();
hm.put("1","A");
hm.put("2","B");
hm.put("3","C");
method1(hm);
}
public void method1(HashMap<String,String> 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<String,String> mapInFile=(HashMap<String,String>)ois.readObject();
ois.close();
fis.close();
//print All data in MAP
for(Map.Entry<String,String> 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<String,String> hm;
public A(){
hm=new HashMap<String,String>();
hm.put("1","A");
hm.put("2","B");
hm.put("3","C");
method2(hm);
}
public void method2(HashMap<String,String> 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<String,String> 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<String,String> mapInFile=new HashMap<String,String>();
//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<String,String> 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