欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

英汉词典程序

程序员文章站 2022-05-04 12:07:30
...
public class Dictionary {
    static private Map<String, String > d= new HashMap();
    static private int size;

    public static int getSize(){
    	return size;
    	}

    public static void insertPare(String Eng, String Ch){
        d.put(Eng, Ch);
        size ++;
    }

    public static void flushToFile() throws IOException{

        File f = new File("d.dat");
        FileOutputStream fo = new FileOutputStream(f);
        ObjectOutputStream oo = new ObjectOutputStream(fo);

        oo.writeObject(d);

        oo.flush();
        oo.close();
        fo.close();
    }

    public static Map<String, String> getFromFile(String fn)
                            throws IOException, ClassNotFoundException{

        FileInputStream fi = new FileInputStream(fn);
        ObjectInputStream oi = new ObjectInputStream(fi);

        Map<String, String> di = (Map<String, String>) oi.readObject();

        fi.close();
        oi.close();

        return di;
    }

    public static String query(String Eng)
            throws IOException, ClassNotFoundException{

        Map<String, String> map = getFromFile("d.dat");
        return map.get(Eng);
    }

    public static void main(String[] args){
        insertPare("amnesty", "赦免");
        insertPare("torture","虐待");
        insertPare("scandal","丑闻");
        try {
            flushToFile();

            System.out.print("请输入要查询的词: ");
            Scanner sc = new Scanner(System.in);
            String r = sc.nextLine();

            System.out.println(query(r));
        }catch (Exception e){}

    }
}