成语大全网 - 汉语词典 - java编写词典代码

java编写词典代码

等我,就写出来

import java.io.*;

import java.util.HashMap;

import java.util.Map.Entry;

public class Test {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

try {

BufferedReader br = new BufferedReader(new FileReader("C:\\text.txt"));//我就不写有界面的了,这个参数就是你英文句子文件所在位置

String sentence = null;

HashMap<String,Integer> map = new HashMap<String, Integer>();

while((sentence = br.readLine())!=null){

sentence = sentence.replaceAll("[\\pP'$']", "");

String[] words = sentence.split(" ");

for(String word:words){

if(map.get(word)==null)

map.put(word, 1);

else map.put(word, map.get(word)+1);

}

}

java.util.Iterator<Entry<String, Integer>> iter = map.entrySet().iterator();

while(iter.hasNext()){

Entry entry = (Entry) iter.next();

System.out.println(entry.getKey()+" "+entry.getValue());

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}