要求 输入英文翻译成中文
输入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 欢迎
时间: 2024-10-02 10:07:10