(第二次作业)运用Java统计字符数、单词数、行数

---恢复内容开始---

                                           Gitee项目地址:项目地址

                                      https://gitee.com/ZK154/WordCount.git                         

1.解题思路

       

首先,当我读完作业要求后,我有了一个大概的思路。   这是一个IO流的问题,有文件的读与写,有以下几点需要注意:

  •  判断关键输入符是否存在(-c , -w ,-l , -o)。
  •    判断读入文件是否存在 。
  •    判断控制台输入格式是否正确 。
  •    对文件内容要用正则表达式筛选。

     这几点分开看并不难,但是合起来必要乱。对文件中其他字符、字母的查找、汉字的去除要用到正则表达式,因为要求生成.exe可执行文件,所以我选择java编程

  

2.准备工作

  •  下载Myeclipse / eclipse / Sublime Text3  任选一样
  •  下载JDK , java编程的核心 。    JDK下载地址
  •    下载exe4j 用于生成exe文件 。 exe4j下载

3.实现编码

  为了实现功能,我写了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文件名,并返回

说明:

         控制台输入进来的就是一个字符串,例如“wc.exe -w test.c ”,我们想要获得要查询得文件名“test.c”就要对字符串进行分割 java里Object.split( )可以实现,返回一个数组。同理 “-w”,"-c","-l","-o"这些关键字也可以获得,这样根据这些字符就可以进行功能的实现。

4.具体代码

实现对字符的统计

 按字节读取先存入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     }
characterNum

实现对单词的统计

题目要求"," 和" "分格的算单词,先用正则表达式过滤汉字,再将"," 替换成空格,这样就能分清哪些是单词,在用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     }
wordNum

实现对行数的统计

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     }
lineNum

实现对默认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     }
writeResult

实现对指定文本的写入

接受目标文件名,并获得路径,,创建出的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     }
writeFile

4.代码测试

  测试文件“test.c”,写入文件output.txt,默认的result.txt与wc.exe都在同一目录下。

   

测试对行数、字符数、单词数单独查询

 

测试关键字符的联合查询,因为是添加模式,result.txt里信息没有被覆盖

 

测试对指定文件的写入(-o)

测试代码能否辨别错误:

 5.单元测试

  将程序中最小的模块拿出来进行测试,

 5.1对characterNum测试:结果正确无误

5.2 对wordNum进行测试:符合结果通过

5.3 对lineNum函数进行测试:

5.4 对writeFile函数的测试:

5.5 对writeResult函数的测试:

测试结果:各函数在单独执行下都能完成功能

6.参考资料

             

            1. 如何将jar打包成exe文件

                       2.Java io流最详细讲解

                 3.JAVA String类常用方法详解

                 4.  关于Java的单元测试

---恢复内容结束---

猜你喜欢

转载自www.cnblogs.com/ZK154/p/9693803.html