要求 輸入英文翻譯成中文
輸入help輸出所有單詞
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.util.Iterator; import java.util.Map; import java.util.Scanner; import java.util.TreeMap; public class ReadDic { public static String readDicFile(String filePath) { String result = ""; try { String encoding = "GBK"; File file = new File(filePath); if (file.isFile() && file.exists()) { //判斷文件是否存在 InputStreamReader read = new InputStreamReader( new FileInputStream(file), encoding);//考慮到編碼格式 BufferedReader bufferedReader = new BufferedReader(read); String dicFullText = null; while ((dicFullText = bufferedReader.readLine()) != null) { result = dicFullText; } read.close(); } else { System.out.println("找不到指定的文件"); } } catch (Exception e) { System.out.println("讀取文件內容出錯"); e.printStackTrace(); } return result; } /** * @param args */ public static void main(String[] args) { Scanner cin = new Scanner(System.in); String input = cin.next(); String dicPath = "D:/zhangyayun 13057655618/Dic/doc.txt"; String result = readDicFile(dicPath); String dicText[] = result.split("\\|"); TreeMap tm = new TreeMap(); for (int i = 0; i < dicText.length; i++) { String temp = dicText[i]; String tempArray[] = temp.split("="); tm.put(tempArray[0], tempArray[1]); } if (input.equals("help")) { Iterator it = tm.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); Object key = entry.getKey(); Object value = entry.getValue(); System.out.println(key + " " + value); } } else { String dicResult = (String) tm.get(input); if (dicResult != null) { System.out.println(tm.get(input)); } else { System.out.println("input error"); } } } }
help
hello 你好
man 男人
welcome 歡迎