package word; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; public class Text { public static void main(String[] args) throws Exception { //BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("piao.txt"))); File word=new File("F:\JAVA\JAVA\新建文件夹\word\piao.txt"); BufferedReader br=new BufferedReader(new FileReader(word)); StringBuffer d=new StringBuffer(); String s=null; String s1=""; while((s=br.readLine())!=null) { d.append(s); } Scanner scanner=new Scanner(word); HashMap<String, Integer > hashMap=new HashMap<String,Integer>(); List<Map.Entry<String, Integer>> list = new ArrayList<>(); while(scanner.hasNextLine()) { String line=scanner.nextLine(); System.out.println(line); String[] lineWords=line.split("\W+"); Set<String> wordSet=hashMap.keySet(); for(int i=0;i<lineWords.length;i++) { if(wordSet.contains(lineWords[i])) { Integer number=hashMap.get(lineWords[i]); number++; hashMap.put(lineWords[i], number); } else { hashMap.put(lineWords[i], 1); } } } for(Map.Entry<String, Integer> entry : hashMap.entrySet()){ list.add(entry); //将map中的元素放入list中 } list.sort(new Comparator<Map.Entry<String, Integer>>(){ @Override public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { return o2.getValue().compareTo(o1.getValue());} //逆序(从大到小)排列,正序为“return o1.getValue()-o2.getValue” }); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i).getKey() + ": " + list.get(i).getValue()); } /*Iterator<String> iterator=hashMap.keySet().iterator(); while(iterator.hasNext()) { //String word1=iterator.next(); //System.out.printf("单词:%-12s 出现次数:%d ",word1,hashMap.get(word1)); }*/ br.close(); BufferedReader br1 = new BufferedReader(new InputStreamReader(new FileInputStream("piao.txt"))); int[] count = new int[26]; int[] COUNT = new int[26]; char[] c = new char[1]; int len = br1.read(c); while(len!=-1) { if(c[0]<='Z'&&c[0]>='A') { int number = c[0]; COUNT[number-65]++; } if(c[0]<='z'&&c[0]>='a') { int number = c[0]; count[number-97]++; } len = br1.read(c); } Print(count, COUNT); System.out.print(" b.txt文件读取完毕!"); br1.close(); } public static void Print(int[] count,int[] COUNT) { double sum=0; char[] c=new char[26]; double[] p=new double[26]; for(int j=0;j<26;j++) { if(count[j]>0&&COUNT[j]>0) { sum=sum+count[j]+COUNT[j]; } } System.out.println("总数为:"+sum); for(int i=0;i<26;i++) { if(count[i]>0) { char lowerCase = (char)(i+97); System.out.println(lowerCase+"("+count[i]+")"); double percent; percent=count[i]/sum; c[i]=lowerCase; //System.out.println("百分比为:"+percent); } if(COUNT[i]>0) { char upperCase = (char)(i+65); System.out.println(upperCase+"("+COUNT[i]+")"); double percent1; percent1=COUNT[i]/sum; //System.out.println("百分比为:"+percent1); } if(count[i]>0&&COUNT[i]>0) { System.out.println("总数为:"+"("+ (count[i]+COUNT[i]) +")"); double percent2; percent2=(count[i]+COUNT[i])/sum; p[i]=percent2; System.out.println("百分比为:"+String.format("%.2f", percent2)); } } double t; char c1; for(int q=0;q<26;q++) { for(int w=q+1;w<26;w++) { if(p[q]<p[w]) { t=p[q]; p[q]=p[w]; p[w]=t; c1=c[q]; c[q]=c[w]; c[w]=c1; } } } for(int q=0;q<26;q++) { System.out.println(c[q]+":"+String.format("%.2f", p[q])); } } }