控制台实现微型词典

从控制台输入英语单词及单词解释两项数据,把录入的数据追加到文件中。要求提供单词查询功能。用户输入单词后,从单词库文件中查找,如果存在则输出该单词的解释。

注意,单词不能有重复,如果重复则覆盖替换以前的解释数据。

应用map的hashmap动态存储更改文件,使得更改具有即时性,并且涉及到相关文件读写流操作,以及格式转换判断等,结束时根据查询词库自动保存文件

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class Dictionary {
    private static Map<String, String> dictionaryMap = new HashMap<>();
    private static boolean isHave = true;


    public static void main(String[] args) throws IOException {

        File file = new File("dictionary.txt");
        if (file.exists() && file.isFile()) {
            System.out.println("文件已存在");
        } else {
            if (file.createNewFile()) System.out.println("创建成功");

        }
        InputStreamReader inputStreamReader = new InputStreamReader(System.in);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReaderForFile = new BufferedReader(fileReader);
        String stringFromFile = null;
        while ((stringFromFile = bufferedReaderForFile.readLine()) != null) {
            String[] strings = stringFromFile.split("=");
            dictionaryMap.put(strings[0], strings[1]);
        }


        PrintWriter printWriter = new PrintWriter(file);
        String s = null;
        boolean b = true;
        System.out.println("微词典fromDUTZhaoPeng\n=====================请选择===============\n1\t存单词解释;\n2\t查询单词\n3\t退出");

            while ((s = bufferedReader.readLine()) != null) {

                if (s.equals("3")) {

                    b = false;break;
                }
                if (s .equals( "1")) {
                    System.out.println("格式: /单词/=/解释/");
                    if ((s = bufferedReader.readLine()) != null) {
                        if ((s.indexOf('=') == -1) || (s.indexOf('=') == 0) || (s.indexOf('=') != s.lastIndexOf('='))) {
                            System.out.println("格式错误");
                            break;
                        }
                        DmapAdd(s);
                    }
                } else if (s.equals("2")) {
                    isHave = false;
                    System.out.println("请输入单词:");
                    if ((s = bufferedReader.readLine()) != null) {
                        String finalS = s;
                        dictionaryMap.forEach((k, v) -> {
                            if (k.equals(finalS)) {
                                System.out.println("解释:" + v);
                                isHave = true;
                            }
                        });
                        if (!isHave) System.out.println("查无此词");

                    }
                }

            }

                dictionaryMap.forEach((k,v)->{
                    printWriter.println(k + "=" + v);
                });


        bufferedReader.close();
        bufferedReaderForFile.close();
        printWriter.close();



    }

    private static void DmapAdd(String s) {
        isHave = true;
        String[] strings = s.split("=");
        dictionaryMap.forEach((k, v) ->{
            if (k.equals(strings[0])) {
                dictionaryMap.put(k, strings[1]);
                isHave = false;
            }
        });
        if (isHave) {
            dictionaryMap.put(strings[0], strings[1]);
        }

    }
}
更多可以参见 我的GitHub

猜你喜欢

转载自blog.csdn.net/zp_icenow/article/details/81040903