1. 程式人生 > >好程式設計師Java分享Java案例_英漢字典

好程式設計師Java分享Java案例_英漢字典

好程式設計師Java分享Java案例_英漢字典,在寫程式碼的時候,當滑鼠懸浮在某一個單詞上面的時候,有道詞典點有時會彈出一個訊息氣泡,在裡面中給出關於這個單詞相關的解釋,下面給大家展示一個使用Java基礎語言編寫的英漢字典案例:
    
  實現功能:
        輸入英文,給出對應的中文翻譯,如果沒有這個單詞沒有被收錄會有相關提示
        
  程式碼編寫環境
        JDK:1.8.0_191
        Eclipse:2019-03 (4.11.0)
        
  素材:
        dict.txt
            字典資源文字檔案,儲存一些下列格式的檔案,英文和翻譯之間用製表符隔開:
  Africa    n. 非洲
Aids    n. 愛滋病
America    n. 美洲
April    n. 四月

  案例實現用到的技術:
      IO流
      Map—HashMap
      字串分割
      異常處理
      
  程式碼思路
1、根據字典檔案路徑,建立file物件
2、判斷file物件是否為空,不為空就繼續,否則直接返回null
3、File不為空,建立InputStreamReader和BufferedReader物件
4、迴圈讀取字典文字中的內容,切割得到陣列,儲存在map中
5、提示輸入單詞,查詢單詞,輸出查詢結果
  
  執行效果
開始執行的提示:
圖片3

查詢成功的反饋
圖片4
單詞不存在的反饋
圖片5
案例程式碼:

編寫方法讀取文字中的內容
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;