编写一个程序,统计给定文件中包含的每个单词出现的频率,并按单词表的顺序显示统计结果

下面代码是从控制台读入数据

package test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;



public class Test{	
	
	public static void main(String[] args) {
		class Word{
			String name = "";
			int count = 0;
			public Word(String name,int count) {
				this.name = name;
				this.count = count;
			}
		}
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		List words = new ArrayList();
				
		Comparator com = new Comparator(){
			@Override
			public int compare(Word o1, Word o2) {				
				return o1.name.compareTo(o2.name);
			}
			
		};
		
		try {
			String line = br.readLine().replaceAll("[^a-zA-Z]+", " ");
			String[] sarr = line.split(" ");
			for(String str : sarr){				
				if(words.size()<1){ 
					words.add(new Word(str,1));
					continue; 
				};
				int i = 0;
				for(;i

 

你可能感兴趣的:(java)