---恢复内容开始---
首先,当我读完作业要求后,我有了一个大概的思路。 这是一个IO流的问题,有文件的读与写,有以下几点需要注意:
这几点分开看并不难,但是合起来必要乱。对文件中其他字符、字母的查找、汉字的去除要用到正则表达式,因为要求生成.exe可执行文件,所以我选择java编程
为了实现功能,我写了6个静态函数:
int linewNum(String fileName) //接受文件名,返回行数, int wordNum(String fileName ) //接受文件名 ,返回单词数, int characterNum(String fileName ) //接受文件名,返回字符数, void writeFile(String targetFile, String save) //接受目标文件与要保存的数据 void writeResult(String string) //接受保存的数据,保存进默认的result.txt文件 String selectC(String[] str, String target) //查询.c文件名,并返回
实现对字符的统计
按字节读取先存入char数组里,再进行统计,注意:回车包含两个符号"\r\n"
1 public static int characterNum(String fileName) throws IOException { 2 File file = new File(fileName); 3 int x = 0; 4 if (file.exists()) { 5 String path = file.getAbsolutePath();// 得到exe的文件路径 6 FileReader fr = new FileReader(path); 7 BufferedReader br = new BufferedReader(fr); 8 String str; 9 char[] ch = new char[300000]; 10 int len = 0; 11 12 while ((len = br.read(ch)) != -1) { 13 x = len; 14 // System.out.println(ch[len]); 15 } 16 fr.close(); 17 br.close(); 18 System.out.println("字符数:" + x); 19 writeResult("字符数:" + x + "\r\n"); 20 if (!(x != 0)) { 21 x = x + 1; 22 } 23 } 24 25 return x; 26 }
实现对单词的统计
题目要求"," 和" "分格的算单词,先用正则表达式过滤汉字,再将"," 替换成空格,这样就能分清哪些是单词,在用split按空格分割,单词就出来了
1 // 单词数 2 public static int wordNum(String fileName) throws IOException { 3 File file = new File(fileName); 4 int total = 0; 5 if (file.exists()) { 6 String path = file.getAbsolutePath();// 得到exe的文件路径 7 FileReader fr = new FileReader(path); 8 BufferedReader br = new BufferedReader(fr); 9 String str; 10 int line = 0; 11 ArrayList array = new ArrayList(); 12 while ((str = br.readLine()) != null) { 13 str = str.replaceAll("[(\\u4e00-\\u9fa5)]", "");// 去除汉字 14 str = str.replaceAll(",", " "); // 去除空格 15 String[] str1 = str.split("\\s+"); // 按空格分割 16 array.add(str1); // 放入集合 17 line++; 18 } 19 fr.close(); 20 br.close(); 21 22 String regex = ".*[a-zA-Z]+.*"; // 正则判断每个数组中是否存在有效单词(存在字母) 23 Pattern p = Pattern.compile(regex); 24 Iterator it = array.iterator(); 25 while (it.hasNext()) { 26 String[] string = (String[]) it.next(); 27 for (int y = 0; y <= string.length - 1; y++) { 28 Matcher m = p.matcher(string[y]); 29 if (m.matches()) { 30 total++; // 每存在一个total加1 31 } 32 } 33 } 34 System.out.println("单词数:" + total); 35 writeResult("单词数:" + total + "\r\n"); 36 if (!(total != 0)) { 37 total = total + 1; 38 } 39 } 40 41 return total; 42 }
实现对行数的统计
java File类自带函数readline可以按行读取,定义一个int形变量 ,每读一行则加一
1 // 行数 2 public static int lineNum(String fileName) throws IOException { 3 File file = new File(fileName); 4 int line = 0; 5 if (file.exists()) { 6 String path = new File(fileName).getAbsolutePath();// 得到exe的文件路径 7 FileReader fr = new FileReader(path); 8 BufferedReader br = new BufferedReader(fr); 9 while (br.readLine() != null) { // 按行读取,每存在一行line+1 10 line++; 11 } 12 fr.close(); 13 br.close(); 14 System.out.println("行数:" + line); 15 writeResult("行数:" + line + "\r\n"); 16 if (!(line != 0)) { 17 line = line + 1; 18 } 19 } 20 21 return line; 22 }
实现对默认result.txt的写入
现获取result.txt的地址,没有的话java里的FileWrite自动生成result.txt文件
1 // 自动写入result.txt文件中 2 public static void writeResult(String string) throws IOException { 3 4 String path = new File("result.txt").getAbsolutePath(); 5 FileWriter fw = new FileWriter(path, true); 6 fw.write(string); 7 if (fw != null) { 8 fw.close(); 9 } 10 }
实现对指定文本的写入
接受目标文件名,并获得路径,,创建出的filewrite对象使用write( )方法存入接受的信息字符串
1 // 写进指定文件 2 public static void writeFile(String targetFile, String save) throws IOException { 3 4 String path = new File(targetFile).getAbsolutePath(); // 得到目标文件路径 5 FileWriter fw = new FileWriter(path, true); 6 fw.write(save); 7 if (fw != null) { 8 fw.close(); 9 System.out.println("存储成功!"); 10 } 11 12 }
测试文件“test.c”,写入文件output.txt,默认的result.txt与wc.exe都在同一目录下。
测试对行数、字符数、单词数单独查询
测试关键字符的联合查询,因为是添加模式,result.txt里信息没有被覆盖
测试对指定文件的写入(-o)
测试代码能否辨别错误:
将程序中最小的模块拿出来进行测试,
5.1对characterNum测试:结果正确无误
5.2 对wordNum进行测试:符合结果通过
5.3 对lineNum函数进行测试:
5.4 对writeFile函数的测试:
5.5 对writeResult函数的测试:
测试结果:各函数在单独执行下都能完成功能
6.参考资料
4. 关于Java的单元测试
---恢复内容结束---
原文:https://www.cnblogs.com/ZK154/p/9693803.html