⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mydictionary.java

📁 This book code procedure according to the chapter arrangement, the 1st chapter of example under "1"
💻 JAVA
字号:
/* * MyDictionary.java * * Created on 2005年11月15日, 下午10:34 * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */package myapp;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.InputStreamReader;/** * * @author Admin */public class MyDictionary {    BufferedReader dic = null;    /** Creates a new instance of MyDictionary */    public MyDictionary() {        try{   //获取字典文件对象。并将其放入缓冲器中。   dic = new BufferedReader(new FileReader("dict.dat"));}catch(FileNotFoundException fe){    System.out.println(fe);}    }    /**  * 查找输入的英文的中文意思  * @param word 指定要查的英文  * @return String 查到的中文。  *  如果有错误发生,则返回null。如果没有找到对于的英文,则返回空字符。  */    private String lookup(String word){    try{       while(true){       	 //读取文件中的一行数据         String sLine = dic.readLine();         //如果到了文件末尾,则退出。         if(sLine==null) break;         //如果数据没有“|”,说明数据有问题。返回null         if(sLine.indexOf("|")==-1){            return null;         }         //分解英文和中文         String sWord = sLine.substring(0,sLine.indexOf("|"));         String sContent = sLine.substring(sLine.indexOf("|")+1);         //判断用户查找的是否相同英文         if(word.equalsIgnoreCase(sWord)){            return sContent;         }       }        return "";    }catch(IOException ie){    	System.out.println(ie);    	close();    	return "";    }  }    /**  *关闭字典文件流,释放资源。  */    private void close(){    try{      if(dic!=null)        dic.close();    }catch(IOException ie){      System.out.println(ie);    }  }    /**     * @param args the command line arguments     */    public static void main(String[] args) {        MyDictionary dict = new MyDictionary();     try{      System.out.print("输入英文:");      //构造一个标准输入流,接受键盘输入。      BufferedReader stdin =new BufferedReader(new InputStreamReader(System.in));      //读取一行数据      String input = stdin.readLine();      //搜索中文意思      String sContent = dict.lookup(input);      //输出结果      System.out.println(sContent);       dict.close();   }catch(IOException ie){      System.out.println(ie);   }    }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -