用java实现的英汉词典

时间:2021-10-22 15:55:50
import java.io.*;
import java.util.*; public class MyDictionary {
static private Map<String, String > dict= new HashMap();
static private int size; public static int getSize(){return size;} public static void insertPare(String EN, String CN){
dict.put(EN, CN);
size ++;
} public static void flushToFile() throws IOException{ File file = new File("dict.dat");
FileOutputStream fop = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fop); oos.writeObject(dict); oos.flush();
oos.close();
fop.close();
} public static Map<String, String> getFromFile(String fileName)
throws IOException, ClassNotFoundException{ FileInputStream fis = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fis); Map<String, String> dict = (Map<String, String>) ois.readObject(); fis.close();
ois.close(); return dict;
} public static String query(String EN)
throws IOException, ClassNotFoundException{ Map<String, String> map = getFromFile("dict.dat");
return map.get(EN);
} public static void main(String[] args){
insertPare("amnesty", "赦免");
insertPare("torture","虐待");
insertPare("scandal","丑闻");
try {
flushToFile(); System.out.print("your query: ");
Scanner scan = new Scanner(System.in);
String read = scan.nextLine(); System.out.println(query(read));
}catch (Exception e){} }
}

真可谓是人生不易,小学期过得艰难。