import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.text.NumberFormat;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;import java.util.Map;import java.util.Map.Entry;import java.util.Scanner;import java.util.TreeMap;public class Flutter { private static MapmyTreeMap = null; private static List > list = null; public static void main(String[] args) { int n = 0; Scanner in = new Scanner(System.in); String filename = "E:\\Homework\\大二下\\软工\\飘英文版.txt"; do { System.out.println("1.显示单词并排序 2.显示字母频率 3.指定显示前n个英文单词 4.停用词 0.退出"); n = in.nextInt(); switch(n) { case 2:frequency();break; case 1: readAndSave(filename); sort(); for(Map.Entry map : list) { if((!map.getKey().equals("the"))||(!map.getKey().equals("a"))) System.out.println(map.getKey() + " :\t" + map.getValue()); } //System.out.println("耗时:" + (System.currentTimeMillis() - t1) + "ms");break; break; case 3: { System.out.println("输入数字"); int num = in.nextInt(); int c = 0; readAndSave(filename); sort(); for(Map.Entry map : list) { if((!map.getKey().equals("the"))||(!map.getKey().equals("a"))) System.out.println(map.getKey() + " :\t" + map.getValue()); c++; if(c >= num) break; } break; } case 4: { readAndSave(filename); sort(); stop(); for(Map.Entry map : list) { if((!map.getKey().equals("the"))||(!map.getKey().equals("a"))) System.out.println(map.getKey() + " :\t" + map.getValue()); } break; } } }while(n!=0); } public static void readAndSave(String filename) { //long t1 = System.currentTimeMillis(); String s; BufferedReader br = null; try { br = new BufferedReader(new FileReader(filename)); StringBuffer sb = new StringBuffer(); //将文件内容存入StringBuffer中 while((s = br.readLine()) != null) { sb.append(s); } //不区分大小写 String str = sb.toString().toLowerCase(); //分隔字符串并存入数组 (以,。空格分割) String[] elements = str.split("[,.“” ‘’?\\s]"); int count = 0; myTreeMap = new TreeMap (); //遍历数组将其存入Map 中 for(int i = 0; i < elements.length; i++) { if(myTreeMap.containsKey(elements[i])) { count = myTreeMap.get(elements[i]); myTreeMap.put(elements[i], count + 1); } else { myTreeMap.put(elements[i], 1); } } //直接遍历Map输出 /*for(Map.Entry entry:myTreeMap.entrySet()){ System.out.println(entry.getKey()+" :\t"+entry.getValue()); }*/ //对频率从大到小排序,然后输出 }catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { br.close(); }catch(Exception e) { e.printStackTrace(); } } } public static void sort() { //将map.entrySet()转换成list list = new ArrayList >(myTreeMap.entrySet()); //通过比较器实现排序 Collections.sort(list, new Comparator >() { //降序排序 public int compare(Entry o1, Entry o2) { return o2.getValue().compareTo(o1.getValue()); // return o1.getValue().compareTo(o2.getValue()); //升序排序 } }); } private static String formattedDecimalToPercentage(double decimal) { //获取格式化对象 NumberFormat nt = NumberFormat.getPercentInstance(); //设置百分数精确度2即保留两位小数 nt.setMinimumFractionDigits(2); return nt.format(decimal); } public static void frequency() { String a1; char a='A'; int a2[]=new int[27]; char b1[]=new char[26]; char b2[]=new char[26]; for(int i=0;i<26;i++) { b1[i]=a; b2[i]=(char)(a+32); a++; } /*for(int i=0;i<26;i++) { System.out.println(b1[i]); System.out.println(b2[i]); }*/ try { BufferedReader in = new BufferedReader(new FileReader("E:\\\\Homework\\\\大二下\\\\软工\\\\飘英文版.txt")); String str; while ((str = in.readLine()) != null) { // System.out.println(str); char[] d=str.toCharArray(); for(int i=0;i stopword = new ArrayList(); while ((str = br_stop.readLine()) != null) { //使用readLine方法,一次读一行 读取停用词 stopword.add(str); } br_stop.close(); myTreeMap.remove(stopword); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}