用java实现的英汉词典

2018/07/17 08:23
阅读数 164
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){}

    }
}

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

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部