package ioTest; import java.io.*; import java.util.*; import java.util.LinkedList; import java.util.List; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * 21.编写一个Java应用程序,使用RandomAccessFile流统计Hello.txt中的单词,要求如下: (1)计算全文中共出现了多少个单词(重复的单词只计算一次); (2)统计出有多少个单词只出现了一次; (3)统计并显示出每个单词出现的频率,并将这些单词按出现频率高低顺序显示在一个TextArea中。(本题30分) * * @since 2010-9-26 * @start 9:20-9:54 12:58-1:49 1:44-2:08 * @end * */ public class TwentyOne_SumWords { public static void main(String[] args){ try{ GUI_SumWords gui=new GUI_SumWords(); gui.init(); gui.start(); } catch(Exception e){} } } class SumWords{ /** * sum words in file and return a map * */ public static Map<String ,WordsWithTimes> sumWords(File file) throws Exception{ TreeMap<String ,WordsWithTimes> map=new TreeMap<String ,WordsWithTimes>(); //read file to string buffer RandomAccessFile raf=new RandomAccessFile(file,"r"); String str=raf.readLine(); StringBuffer sb=new StringBuffer(); while(str!=null){ sb.append(str); sb.append(" "); str=raf.readLine(); } //split to array String[] words=sb.toString().split(" "); int count=1; WordsWithTimes wwt=null; for(int i=0;i<words.length;i++){ if(map.get(words[i])==null){ map.put(words[i], new WordsWithTimes(words[i],new Integer(1))); } else{ wwt=map.get(words[i]); if(wwt!=null){ count=wwt.getTimes(); } count++; map.put(words[i], new WordsWithTimes(words[i],new Integer(count))); } } //make clear words=null; sb=null; raf.close(); return map; } public static List sumWord(File file)throws Exception{ List list=new ArrayList(); //read file to string buffer RandomAccessFile raf=new RandomAccessFile(file,"r"); String str=raf.readLine(); StringBuffer sb=new StringBuffer(); WordsWithTimes wwt=null; while(str!=null){ sb.append(str); sb.append(" "); str=raf.readLine(); } //split to array String[] words=sb.toString().split(" "); int count=1; if(words.length>0){ list.add(new WordsWithTimes(words[0],1)); } for(int i=1;i<words.length;i++){ boolean update=false; { //find same key for(int j=0;j<list.size();j++){ wwt=(WordsWithTimes)(list.get(j)); if(wwt.getWord().equals(words[i])){ wwt.setTimes(wwt.getTimes()+1); update=true; break; } } if(!update){ list.add(new WordsWithTimes(words[i],1)); } } } return list; } /** * count number of words just appearance once * */ public static int countOnceAppearanceWords(Map<String ,WordsWithTimes> map) throws Exception{ int count=0; WordsWithTimes wwt=null; for(int i=0;i<map.size();i++){ wwt=map.get(i); if(wwt!=null){ if(wwt.getTimes()==1){ count++; } } } return count; } public static int countOnceAppearanceWord(List<WordsWithTimes> list) throws Exception{ int count=0; WordsWithTimes wwt=null; for(int i=0;i<list.size();i++){ wwt=list.get(i); if(wwt.getTimes()==1){ count++; } } return count; } /** * list of words order by appearance times and ASC * */ public static List solt(Map map){ LinkedList list=new LinkedList(); WordsWithTimes wwt=null; WordsWithTimes temp=null; for(int i=0;i<map.size();i++){ wwt=(WordsWithTimes)map.get(i); if(wwt!=null){ if(list.size()==0){ list.add(wwt); } else{ for(int j=0;j<list.size();j++){ temp=(WordsWithTimes)list.get(i); if(wwt.getTimes()>=temp.getTimes()){ list.add(i,wwt); break; } else{ if(j==list.size()-1){ list.add(wwt); } } } } } } return list; } public static List sortList(List<WordsWithTimes> input) throws Exception{ List<WordsWithTimes> in=new ArrayList(); in=input; List out =new LinkedList(); WordsWithTimes wwt=null; WordsWithTimes temp=null; wwt=in.get(0); int count =in.size(); for(int i=0;i<count;i++){ for(int j=1;j<in.size();j++){ temp=in.get(j); wwt=temp.getTimes()>wwt.getTimes()?temp:wwt; } out.add(wwt); in.remove(wwt); if(in.size()>0){ wwt=in.get(0); } } return out; } } class WordsWithTimes{ private String word; private Integer times; WordsWithTimes(String word,Integer times){ this.word=word; this.times=times; } public String getWord() { return word; } public void setWord(String word) { this.word = word; } public Integer getTimes() { return times; } public void setTimes(Integer times) { this.times = times; } public String toString(){ return this.word+","+this.times; } } class GUI_SumWords extends JFrame{ JTextArea area=new JTextArea(); JLabel label=new JLabel("File path"); JTextField field=new JTextField("src/ioTest/Input.txt"); JButton btn=new JButton("Show!"); List list=null; public void init(){ area.setColumns(20); area.setRows(10); this.setBounds(111, 111, 800, 600); this.setLayout(new GridLayout(1,2)); this.add(area); JPanel panel=new JPanel(); panel.setLayout(new GridLayout(3,1)); panel.add(label); panel.add(field); panel.add(btn); this.add(panel); this.setVisible(true); this.validate(); } public void start(){ btn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ try{ area.setText(""); List list=SumWords.sumWord(new File(field.getText().trim())); int count=SumWords.countOnceAppearanceWord(list); List list2=SumWords.sortList(list); label.setText("words count=`"+count); for(int i=0;i<list2.size();i++){ area.append(list2.get(i).toString()); area.append("/r/n"); } } catch(Exception ee){ ee.printStackTrace(); } } }); } }