源程序:
Text_2.java
import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.InputStreamReader;import java.text.DecimalFormat;import java.text.NumberFormat;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.Scanner;public class Text_2 {static int N=5;public static String StatList(String str) { StringBuffer sb = new StringBuffer(); HashMaphas = 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 judge; int n; Scanner in=new Scanner(System.in); System.out.println("输入前n个最常出现的单词:"); n=in.nextInt(); for(int i=0;i fil=getFiles("a");//目录下的文件// ArrayList fil=getDirectory(file);//目录下的文件和子目录里的文件// for(int i=0;i 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; } public static void zimu(String path) { try { //IO操作读取文件内容 FileReader fr = new FileReader(path); BufferedReader br = new BufferedReader(fr); DecimalFormat df = new DecimalFormat("#.00"); HashMap map = new HashMap (); String string =null; Integer count = 0;//每个字母的次数 Integer total = 0;//总共多少个字母 try { 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); } } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ArrayList list = new ArrayList (); list.addAll(map.keySet()); //将单词添加到list中 NumberFormat numberFormat = NumberFormat.getInstance(); // 设置精确到小数点后2位 numberFormat.setMaximumFractionDigits(2); //排序按照出现次数降序排列 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)) +" "+ df.format(((float)map.get(list.get(i)))*100/total) + "%"); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 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]; int judge; int n=20; for(int i=0;i 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[2000]; int s[]=new int[2000]; int judge; int n=1000; String duanyu=""; for(int i=0;i
writeFromFile.java
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("b.txt"); FileOutputStream fos=new FileOutputStream(file); OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8"); osw.append(a); osw.close(); fos.close(); }}
实验截图:
实验总结:
这次的实验在上次的基础上,要把上次的功能写成函数,分别进行调用,因为上次的实验我已经将功能写成函数,所以在这次的实验中写了一个界面,对函数进行了分别调用