zoukankan      html  css  js  c++  java
  • 用java实现的英汉词典

    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){}
    
        }
    }

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

    你若笃定,世界便不浮躁。
  • 相关阅读:
    辅助随笔:因知识点不足暂时错过的题目
    NOIP2019翻车前写(and 抄)过的代码
    NOIP2019翻车前计划以及日记
    Luogu P3706 [SDOI2017]硬币游戏
    Luogu P5296 [北京省选集训2019]生成树计数
    Luogu P3307 [SDOI2013]项链
    Gaussian整数
    Problem. S
    LOJ6696 复读机 加强版
    数据库约束
  • 原文地址:https://www.cnblogs.com/zhangyue123/p/9321415.html
Copyright © 2011-2022 走看看