Java Java programmers to share good case _ English-Chinese dictionary

When writing code, when a certain word hovered above him, way dictionary point sometimes will pop up a message bubble, an explanation is given on the relevant word in it, the following to show you a written using Java language foundation English-Chinese dictionary case:   function:         input English, gives the corresponding Chinese translation, if not the word will not be included tips   coding environment         the JDK: 1.8.0_191         the Eclipse: 2019-03 (4.11.0)   material:         dict .txt             dictionary resource text files, save files in the following formats, separated by tabs between the English and the translation:   Africa the n-Africa. Aids the n-AIDS. America the n-America. April the n-April.
    


        



        






  Case technology used to achieve:
      IO stream
      Map-HashMap
      split a string
      exception handling   code that ideas 1, according to the dictionary file paths, create an object file 2, file to determine whether the object is null, not empty continue, otherwise direct return null 3, File does not create InputStreamReader and BufferedReader object is empty, 4, loop reads the contents of the dictionary text, obtained by cutting arrays, stored in the map 5, prompts for word, the word query, the query results output   operating results start running tips:
      






  

Query successful feedback


There is no word of feedback

Case Code:

Write a method to read the contents of the text
package com.feng.demo01;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
 *    英汉字典案例
 * @author dushine
*/
public class GetDict {

    public static void main(String[] args) {
        String path = "E:\\dict.txt";

        // 获取字典中的所有内容
        Map<String, String> dict = getText(path);
        
        // 判断字典是否为空,提示输入单词,获取查询结果
        if (dict != null) {
            @SuppressWarnings("resource")
            
            // 获取输入内容
            Scanner input = new Scanner(System.in);
            System.out.println("请输入要查询的单词:");
            String word = input.next();
            
            // 查询字典获取中文,如果没有也给出反馈
            String ret = dict.get(word);
            if (ret != null) {
                System.out.println("查询结果:\n"+word + ":" + ret);
            } else {
                System.out.println("您查询的单词尚未收录,敬请期待!");
            }
        }
    }

    /**
     * 获取字典文件中内容
     * @param path
     * @return
     */
    private static Map<String, String> getText(String path) {
        // 可能会出现异常
        try {
            // 根据路径创建文件对象
            File file = new File(path);
            
            // 判断路径指向的文件是否存在
            if (file.exists() && file.isFile()) {
                // 创建map,存储读取得到的内容
                Map<String, String> dict = new HashMap<String, String>();
                System.out.println("文件路径正确,正在解析。。。");
                
                    // 创建输入流对象
                    InputStreamReader reader = 
  new InputStreamReader(new FileInputStream(file), "gbk");
                    BufferedReader bufferedReader = new BufferedReader(reader);
                    String text = null;
                    
                    // 循环读取文件内容
                    while ((text = bufferedReader.readLine()) != null) {
                        
                        // 切割每一行内容,得到数组
                        String[] arr = text.split("\t");
                        
                        // 把切割得到的内容放入map
                        dict.put(arr[0], arr[1]);
                    }
                    
                    // 读取结束,关闭流对象并返回结果
                    reader.close();
                    return dict;
                } else {
                System.out.println("字典崩溃啦,下次再来使用吧。。。");
            }
        } catch (Exception e) {
            System.out.println("字典好像出了点问题、文件内容出错啦。。。");
            e.printStackTrace();
        }
        
        // 路径指向的不是文件或者文件不存在返回null
        return null;
    }

Guess you like

Origin www.cnblogs.com/gcghcxy/p/10938946.html