输出单个文件中的前N个最常出现的英文单词
作者:互联网
思路:
从头到尾遍历文件,从文件中读取遍历到每一个单词。
把遍历到的单词放到hash_map中,并统计这个单词出现的次数。
定义全局变量N控制输出多少个单词
循环对全部单词次数进行比较获取出现次数最多的那个,输出后,删除。循环N次。
源代码:
package demo1; //从头到尾遍历文件,从文件中读取遍历到的每一个单词。 //把遍历到的单词放到hash_map中,并统计这个单词出现的次数。 //import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; //import java.io.Reader; //从文本文档中读入 public class writeFromFile { public static String readTxtFile(String filePath) { try { String encoding = "GBK"; File file = new File(filePath); if (file.isFile() && file.exists()) { // 判断文件是否存在 InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; String lineText = ""; while ((lineTxt = bufferedReader.readLine()) != null) { lineText += (lineTxt); } read.close(); return lineText; } else { System.out.println("找不到指定的文件"); } } catch (Exception e) { System.out.println("读取文件内容出错"); e.printStackTrace(); } return null; } public static void daochu(String a) throws IOException { File file = new File("E:/TXT文件夹/daochu.txt"); FileOutputStream fos = new FileOutputStream(file); OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); osw.append(a); osw.close(); fos.close(); } }
1 package demo1; 2 3 import java.io.IOException; 4 import java.util.HashMap; 5 import java.util.Iterator; 6 7 public class Tongji { 8 final static int N = 10; 9 10 public static String StatList(String str) { 11 StringBuffer sb = new StringBuffer(); 12 HashMap<String, Integer> has = new HashMap<String, Integer>(); // 打开一个哈希表 13 String[] slist = str.split("[^a-zA-Z\']+"); 14 for (int i = 0; i < slist.length; i++) { 15 if (!has.containsKey(slist[i])) { // 若尚无此单词 16 // Pattern pa=Pattern.compile("[^a-zA-Z]+"); //正则表达式 匹配字符串 17 // Matcher match=pa.matcher(slist[i]); 18 // if(!match.matches()) 19 has.put(slist[i], 1); 20 } else {// 如果有,就在将次数加1 21 has.put(slist[i], has.get(slist[i]) + 1); 22 } 23 } 24 25 //遍历map 26 Iterator<String> iterator = has.keySet().iterator(); 27 String a[] = new String[10]; 28 int s[] = new int[10]; 29 30 for (int i = 0; i < N; i++) { 31 iterator = has.keySet().iterator(); 32 while (iterator.hasNext()) { 33 String word = (String) iterator.next(); 34 if (s[i] < has.get(word)) { 35 s[i] = has.get(word); 36 a[i] = word; 37 } 38 } 39 sb.append("单词:").append(a[i]).append(" 次数").append(has.get(a[i])).append("\r\n"); 40 has.remove(a[i]); 41 } 42 return sb.toString(); 43 } 44 45 public static void main(String[] args) { 46 // TODO Auto-generated method stub 47 String filePath = "E:/TXT文件夹/Harry.txt"; 48 String sz = writeFromFile.readTxtFile(filePath); 49 String ltxt = null; 50 System.out.println(ltxt = StatList(sz)); 51 try { 52 writeFromFile.daochu(ltxt); 53 } catch (IOException e) { 54 // TODO Auto-generated catch block 55 e.printStackTrace(); 56 } 57 } 58 }
标签:java,String,new,单词,英文单词,io,单个,import,最常 来源: https://www.cnblogs.com/022414ls/p/11795111.html