Java 如何使用HashMap写入和读取文件?
我有一个带有两个字符串的Java 如何使用HashMap写入和读取文件?,java,file,dictionary,hashmap,Java,File,Dictionary,Hashmap,我有一个带有两个字符串的HashMap 现在我想在一个外部文件中保存映射,以便以后使用映射,而无需再次初始化它 那么,我必须如何保存映射以便以后再次使用它呢?HashMap实现了Serializable,这样您就可以使用普通序列化将HashMap写入文件 以下是链接,例如您可以使用ObjectOutputStream中的writeObject将对象写入文件 我能想到的最简单的解决方案是使用Properties类 保存地图: Map<String, String> ldapConten
HashMap
现在我想在一个外部文件中保存映射
,以便以后使用映射
,而无需再次初始化它
那么,我必须如何保存
映射
以便以后再次使用它呢?HashMap
实现了Serializable
,这样您就可以使用普通序列化将HashMap写入文件
以下是链接,例如您可以使用ObjectOutputStream中的
writeObject
将对象写入文件
我能想到的最简单的解决方案是使用Properties类 保存地图:
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());
}
编辑2:
而不是保存示例中的for循环(如OldCurmudgeon所建议的):
properties.putAll(ldapContent);
但是,对于加载示例,这是最好的方法:
ldapContent = new HashMap<Object, Object>(properties);
ldapContent=newhashmap(属性);
由于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();
}
}
注意:上面的代码可能不是完成此任务的最快方法,但我想展示一些类的应用程序
看,,你试过什么?您可以手动使用Java序列化、XML、CSV、二进制数据……我只知道简单的文件阅读器。因此,感谢您提供的序列化提示。我以前不知道……因为
属性
实现了映射
您应该能够使用putAll
来填充它。另外,由于HashMap
有一个HashMap(MapI同意,它简化了解决方案此解决方案不维护Hashmap中的顺序。有什么东西可以从树映射中写入文件,树映射中维护插入顺序吗?很好。它可以工作:D非常感谢!!!+1要查看完整的代码,谢谢欢迎@user1671245,我尝试给出完整的示例,并使用更多的课程来展示不同的方式,我希望它能帮助你
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();
}
}
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();
}
}