package test; 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.HashMap; import java.util.Scanner; public class W1 { static Scanner in = new Scanner(System.in); public static void main(String[] args) throws IOException { deal(); } public static void deal() throws IOException { int choice = 0; while(choice != 5) { System.out.println("请输入选择"); System.out.println("0输出字母"); System.out.println("1输出单词"); System.out.println("2输出前n个单词"); System.out.println("3过滤无用词"); System.out.println("4统计短语"); choice = in.nextInt(); switch (choice) { case 0 : { word(); break; } case 1: { String filePath = "d:\\文件\\单词.txt"; String sz=W3.readTxtFile(filePath); System.out.println(W2.StatList1(sz)); break; } case 2: { String filePath = "d:\\文件\\单词.txt"; String sz=W3.readTxtFile(filePath); System.out.println(W2.StatList2(sz)); break; } case 3: { String filePath = "d:\\文件\\单词.txt"; String sz=W3.readTxtFile(filePath); System.out.println(W2.StatList(sz)); break; } case 4: { String filePath = "d:\\文件\\stopword.txt"; String sz=W3.readTxtFile(filePath); String[] slist = sz.split("[^a-zA-Z\']+"); break; } } } } public static void word() throws IOException { try { //IO操作读取文件内容 FileReader fr = new FileReader("d:\\文件\\单词.txt"); BufferedReader br = new BufferedReader(fr); NumberFormat nf = NumberFormat.getInstance(); // 设置精确到小数点后2位 nf.setMaximumFractionDigits(2); HashMapmap = new HashMap (); String string =null; Integer count = 0;//每个字母的次数 Integer total = 0;//总共多少个字母 while ((string=br.readLine())!=null) { char[] ch = string.toCharArray(); for (int i = 0; i < ch.length; i++) { if (ch[i] > 'A' && ch[i]< 'z') { total++; ch[i] = Character.toLowerCase(ch[i]); count = map.get(ch[i]+""); if (count == null) { count = 1; }else { count++; } map.put(ch[i]+"", count); } } } ArrayList list = new ArrayList (); list.addAll(map.keySet()); //将单词添加到list中 //排序按照出现次数降序排列 for(int i = 0;i < list.size();i++) { for(int j = 0;j < (list.size() - i-1);j++) //list.size() - i-1因为要用到i+1要考虑是否超出范围的问题 { if(map.get(list.get(j)) < map.get(list.get(j+1))) { String t = list.get(j); list.set(j, list.get(j+1)); list.set( j+1, t); } } } for(int i = 0 ; i < list.size();i++) { System.out.println(list.get(i) + ":" + map.get(list.get(i)) +" "+ nf.format((float)(map.get(list.get(i)))*100/total) + "%"); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 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 StatList2(String str) { StringBuffer sb = new StringBuffer(); HashMap has = new HashMap (); // 打开一个哈希表 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 iterator = has.keySet().iterator(); String a[]=new String[100]; int s[]=new int[100]; int n; Scanner in=new Scanner(System.in); System.out.println("输入前n个最常出现的单词:"); n=in.nextInt(); for(int i=0;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; } } sb.append("单词:").append(a[i]).append(" 次数").append(has.get(a[i])).append("\r\n"); has.remove(a[i]); } return sb.toString(); } public static String StatList1(String str) { StringBuffer sb = new StringBuffer(); HashMap has = new HashMap (); // 打开一个哈希表 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 iterator = has.keySet().iterator(); String a[]=new String[100]; int s[]=new int[100]; for(int i=0;i<50;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; } } sb.append("单词:").append(a[i]).append(" 次数").append(has.get(a[i])).append("\r\n"); has.remove(a[i]); } return sb.toString(); } public static String StatList(String str) { StringBuffer sb = new StringBuffer(); HashMap has = new HashMap (); // 打开一个哈希表 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 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 ) { 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 fil=getFiles("d:\\文件"); //目录下的文件 ArrayListfil=getDirectory(file);//目录下的文件和子目录里的文件 for(int i=0;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 filePath = "d:\\文件\\stopword.txt"; String sz=W3.readTxtFile(filePath); String[] slist = sz.split("[^a-zA-Z\']+"); for(int i=0;i ) { if(slist[i].equals(a)) { n=1; } } return n; } public static ArrayList getFiles(String path) { ArrayList files = new ArrayList (); 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 getDirectory(File file) { ArrayList files = new ArrayList (); ArrayList files1 = new ArrayList (); //获取该目录下的文件列表 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(); } }