od HashMap
implementuje interfejs Serializable
, można po prostu użyć klasy ObjectOutputStream
, aby napisać cały plik Map
do pliku, i przeczytać go ponownie za pomocą ObjectInputStream
klasa
poniżej prostego kodu, który wyjaśni użycie ObjectOutStream
i 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();
}
}
lub jeśli chcesz zapisać dane jako tekst do pliku może po prostu iterację Map
i zapisu klucza i wartości linia po linii, a ją ponownie przeczytać wiersz po wierszu i dodać do 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();
}
}
UWAGA: powyższy kod nie może być najszybszym sposobem wykonywania tego zadania, ale chcę pokazać trochę stosowania clas ses
Zobacz ObjectOutputStream, ObjectInputStream, HashMap, Serializable, StringTokenizer
Co próbowałeś? Możesz używać serializacji Java, XML, CSV, danych binarnych ręcznie ... –
Po prostu znam prosty filereader. dlatego też dziękuję za wskazówkę do serializacji. Nie wiem tego wcześniej ... –