首先需要将txt文件中的内容读入,这需要读到文件的存储路径和文件中的内容,然后就可以进行统计。
package Test; /*import java.util.Scanner; import java.io.*; public class Test { private static String str[]=new String[9999999];//存放导入的文件 public static void main(String args[]) { readFile(); //writeFile(); } 读入TXT文件 public static void readFile() { String txt = "Harry Potter and the Sorcerer's Stone.txt"; // 绝对路径或相对路径都可以,写入文件时演示相对路径,读取以上路径的input.txt文件 int i=0; try (FileReader reader = new FileReader(txt); BufferedReader br = new BufferedReader(reader) ) {// 建立一个对象,它把文件内容转成计算机能读懂的语言 String line; while ((line=br.readLine())!=null) { // 一次读入一行数据 str[i]=line; i++; } } catch (IOException e) { e.printStackTrace(); } } 写入TXT文件 public static void writeFile() { try { File writeName = new File("output.txt"); // 相对路径,如果没有则要建立一个新的output.txt文件 writeName.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖 try (FileWriter writer = new FileWriter(writeName); BufferedWriter out = new BufferedWriter(writer) ) { out.write("我会写入文件啦1 "); // 即为换行 out.write("我会写入文件啦2 "); // 即为换行 out.flush(); // 把缓存区内容压入文件 } } catch (IOException e) { e.printStackTrace(); } } }*/ import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; public class Test { public static void main(String[] args)throws IOException//扔掉很重要 { File file = new File("Harry Potter and the Sorcerer's Stone.txt"); //System.out.println("0.统计英文字母个数及其百分比 1.统计英文单词个数"); Scanner sc = new Scanner(System.in); /*int value=sc.nextInt(); while(true) { switch(value) { case 0:txtString(file); case 1:txtString2(file); default:break; } }*/ txtString2(file); } /* * 统计字母 */ public static void txtString(File file) throws IOException{ try { //IO操作读取文件内容 FileReader fr = new FileReader(file); @SuppressWarnings("resource") BufferedReader br = new BufferedReader(fr);//构造一个BufferedReader类来读取文件 HashMap<String, Integer> hm = new HashMap<String, Integer>();//构建了一个新的HashMap对象,强制指定这个HashMap必须是以String为key, 以Integer为值。 String line =null; Integer count = 0;//每个字母的个数 Integer total = 0;//统计字母总数,作百分比用 while ((line=br.readLine())!=null) { char[] ch = line.toCharArray();//将字符串对象中的字符转换为一个字符数组。 total = total + ch.length; for (int i = 0; i < ch.length; i++) { ch[i] = Character.toLowerCase(ch[i]);//将大写字符转换为小写,小写字母不变 count = hm.get(ch[i]+"");//ch[i]+""的作用是加一个空格后,括号内转化为字符串 if (count == null) { count =1;//只出现一次 }else { count++; } hm.put(ch[i]+"", count); } } for (String str : hm.keySet()) {//设变量str获取键 System.out.println(str+"个数:"+hm.get(str)+" "+hm.get(str)*100.0/total+"%"); } System.out.println("总字母个数:"+total); List<Map.Entry<String ,Integer>> list = new ArrayList<Map.Entry<String,Integer>>(hm.entrySet()); //在java中,如果要对集合对象或数组对象进行排序,需要实现Comparator接口以达到我们想要的目标 Comparator<Map.Entry<String,Integer>> comparator = new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> left, Map.Entry<String, Integer> right) { return (left.getValue().compareTo(right.getValue())); } }; // 集合默认升序升序 Collections.sort(list,comparator); for(int i=0;i<10;i++){// 由高到低输出 System.out.println(list.get(list.size()-i-1).getKey() +":"+list.get(list.size()-i-1).getValue()); } } catch (FileNotFoundException e) { e.printStackTrace(); } } /* * 统计单词 */ public static void txtString2(File file) throws IOException{ FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr);//构造一个BufferedReader类来读取文件 StringBuffer sb = new StringBuffer(); String line =null; while ((line=br.readLine())!=null){ sb.append(line);//将读取出的字符追加到stringbuffer中 } fr.close(); // 关闭读入流 String str = sb.toString().toLowerCase(); // 将stringBuffer转为字符并转换为小写 String[] words = str.split("[^(a-zA-Z)]+"); // 非单词的字符来分割,得到所有单词 Map<String ,Integer> map = new HashMap<String, Integer>() ; for(String word :words){ if(map.get(word)==null){ // 若不存在说明是第一次,则加入到map,出现次数为1 map.put(word,1); }else{ map.put(word,map.get(word)+1); // 若存在,次数累加1 } } // 排序 List<Map.Entry<String ,Integer>> list = new ArrayList<Map.Entry<String,Integer>>(map.entrySet()); Comparator<Map.Entry<String,Integer>> comparator = new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> left, Map.Entry<String, Integer> right) { int i=left.getValue()-right.getValue(); if(i==0) { return (right.getKey().compareTo(left.getKey())); } return (left.getValue().compareTo(right.getValue())); } }; // 集合默认升序 Collections.sort(list,comparator); int n=list.size(); System.out.println("共有单词:"+n+"种"); System.out.println("显示出现频率前n的单词,请输入n:"); Scanner scanner=new Scanner(System.in); n=scanner.nextInt(); for(int i=0;i<n;i++){// 由高到低输出 System.out.println(list.get(list.size()-i-1).getKey() +":"+list.get(list.size()-i-1).getValue()); } } }