package com.my;
import java.io.*;
import java.util.*;
public class CountOccurrenceOfWords {
/**
* @param args
*/
public void count(String s)
{
long startTime=System.currentTimeMillis();//记录初始时间以计算程序运行时间
try{
FileReader fr=new FileReader(s);//读取指定位置的字符文件
BufferedReader br=new BufferedReader(fr);//从字符输入流中读取文本,缓冲字符
String string;
String text="";
while((string=br.readLine())!=null)
{
text+=string;//把文本中的字符连字符串成一个
}
TreeMap<String,Integer> map=new TreeMap<String,Integer>();
String[] words=text.split("[ 0123456789\"+-=*\n\t\r.,;:!?(){}]");//单词分隔
for(int i=0;i<words.length;i++)
{
String key=words[i].toLowerCase();//都转成小写字母
if(key.length()>0)
{
if(map.get(key)==null)
{
map.put(key, 1);
}
else
{
int value=map.get(key).intValue();
value++;
map.put(key, value);
}
}
}
Set<Map.Entry<String, Integer>> entrySet=map.entrySet();
File f2=new File("E:/bb.txt");//建一个文本放统计的单词和数量
FileWriter fw=new FileWriter(f2);
BufferedWriter bufw=new BufferedWriter(fw);
for(Map.Entry<String, Integer> entry:entrySet)
{
bufw.write(entry.getKey()+"\t"+entry.getValue());
bufw.newLine();
bufw.flush();
System.out.println(entry.getKey()+"\t"+entry.getValue());
}
bufw.close();
fw.close();
}catch(FileNotFoundException e2){
e2.printStackTrace();
}catch(IOException e1){
e1.printStackTrace();
}
finally{
}
long endTime=System.currentTimeMillis();
long time=endTime-startTime;
System.out.println("运行时间:"+time+"ms");
}
}
下面是用AWT写的一个简单界面
package com.my;
import java.awt.*;
import java.awt.event.*;
public class AWTTest {
Frame f=new Frame("词频统计");
Label l1=new Label(" 源文件位置:");
Label l2=new Label("保存文件位置:");
Label l3=new Label("保留语言种类:");
Button b1=new Button("...");
Button b2=new Button("...");
Button b3=new Button("开始统计");
Button b4=new Button("现在退出");
TextField t1=new TextField();
TextField t2=new TextField();
Choice c1=new Choice();
FileDialog d1=new FileDialog(f,"选择需要统计的文件",FileDialog.LOAD);
FileDialog d2=new FileDialog(f,"选择保存文件的路径",FileDialog.SAVE);
String s1="";
String s2="";
public void init()
{
f.setLayout(null);
l1.setBounds(40, 40, 100, 20);
f.add(l1);
t1.setBounds(140, 40, 300, 20);
f.add(t1);
b1.addActionListener(new ActionListener()//打开需要统计的文章
{
public void actionPerformed(ActionEvent e)
{
d1.setVisible(true);
t1.setText(d1.getDirectory()+d1.getFile());
s1=d1.getDirectory()+d1.getFile();
}
});
b1.setBounds(450, 40, 30, 20);
f.add(b1);
l2.setBounds(40, 80, 100, 20);
f.add(l2);
t2.setBounds(140, 80, 300, 20);
f.add(t2);
b2.addActionListener(new ActionListener()//找到要放统计单词和数量的文本
{
public void actionPerformed(ActionEvent e)
{
d2.setVisible(true);
t2.setText(d2.getDirectory()+d2.getFile());
s2=d2.getDirectory()+d2.getFile();
}
});
b2.setBounds(450, 80, 30, 20);
f.add(b2);
l3.setBounds(40, 120, 100, 20);
f.add(l3);
c1.setBounds(140, 120, 100, 20);
c1.add("ENGLISH");
c1.add("CHINESE");
f.add(c1);
b3.addActionListener(new ActionListener()//统计分析按钮的驱动
{
public void actionPerformed(ActionEvent e)
{
try{
new CountOccurrenceOfWords().count(s1);
}catch(Exception e1){
e1.printStackTrace();
}
}
});
b3.setBounds(100, 200, 60, 20);
f.add(b3);
b4.addActionListener(new ActionListener()//退出按钮的驱动
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
b4.setBounds(350, 200, 60, 20);
f.add(b4);
f.setBounds(30,30,600,400);
f.setVisible(true);
}
public static void main(String[] args) {
AWTTest at=new AWTTest();
at.init();
}
}