单词统计续
1.输出单个文件中的前 N 个最常出现的英语单词。
2.输出文件中所有不重复的单词,按照出现次数由多到少排列,出现次数同样多的,以字典序排列。
3.指定文件目录,对目录下每一个文件执行统计的操作。
4.指定文件目录,是会递归遍历目录下的所有子目录的文件进行统计单词的功能。
package test; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Scanner; public class W2 { public static String StatList(String str) { StringBuffer sb = new StringBuffer(); HashMap<String ,Integer> has = new HashMap<String ,Integer> (); // 打开一个哈希表 String[] slist = str.split("[^a-zA-Z\']+"); for (int i = 0; i < slist.length; i++) { if (!has.containsKey(slist[i])) { has.put(slist[i], 1); } else { has.put(slist[i],has.get(slist[i])+1 ); } } Iterator<String> iterator = has.keySet().iterator(); String a[]=new String[100]; int s[]=new int[100]; int n=15; int judge; Scanner in=new Scanner(System.in); System.out.println("输入前n个最常出现的单词:"); n=in.nextInt(); for(int i=0;i<n;i++) { iterator = has.keySet().iterator(); while(iterator.hasNext()) { String word = (String) iterator.next(); if(s[i]<has.get(word)) { s[i]=has.get(word); a[i]=word; } } judge=woor(a[i]); if(judge==1) { n++; has.remove(a[i]); } else { sb.append("单词:").append(a[i]).append(" 次数").append(has.get(a[i])).append("\r\n"); has.remove(a[i]); } } return sb.toString(); } public static void main(String[] args) { File file = new File("d:\\文件"); // // TODO Auto-generated method stub // ArrayList<String> fil=getFiles("d:\\文件");//目录下的文件 ArrayList<String> fil=getDirectory(file);//目录下的文件和子目录里的文件 for(int i=0;i<fil.size();i++) { String filePath = fil.get(i); //String filePath = "d:\\文件\\单词.txt"; String sz=W3.readTxtFile(filePath); String ltxt=null; System.out.println(ltxt=StatList(sz)); try { W3.daochu(ltxt); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static int woor(String a) { int n=0; String[] biao= {"a","the","an","and"}; for(int i=0;i<biao.length;i++) { if(biao[i].equals(a)) { n=1; } } return n; } public static ArrayList<String> getFiles(String path) { ArrayList<String> files = new ArrayList<String>(); File file = new File(path); File[] tempList = file.listFiles(); for (int i = 0; i < tempList.length; i++) { if (tempList[i].isFile()) { files.add(tempList[i].toString()); } if (tempList[i].isDirectory()) { } } return files; } private static ArrayList<String> getDirectory(File file) { ArrayList<String> files = new ArrayList<String>(); ArrayList<String> files1 = new ArrayList<String>(); //获取该目录下的文件列表 File flist[] = file.listFiles(); for (File f : flist) { if (f.isDirectory()) { // 如果f是一个目录 files1=getFiles(f.getAbsolutePath()); files.addAll(files1); getDirectory(f); } else { //如果f是一个文件 files.add(f.getAbsolutePath()); } } return files; } }
package test; 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 W3 { 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("d:\\文件\\c.txt"); FileOutputStream fos=new FileOutputStream(file); OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8"); osw.append(a); osw.close(); fos.close(); } }
package test; 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 W3 { 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("d:\\文件\\c.txt"); FileOutputStream fos=new FileOutputStream(file); OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8"); osw.append(a); osw.close(); fos.close(); }<br><br> }