1.git项目地址:
https://github.com/StrangeTang/WCProject
2.PSP表格:
|
3.解题思路:
1)基本功能都是一些java编程,最主要涉及到文件的读写操作以及字符串的相关操作
2)git上传很简单,在网上一般都能找到上传的方法
3)jar文件打包网上也有教程,也很简单
4)最后要注意的事args字符串数组的传值问题,实现-c,-l,-w等功能
4.程序实现过程:
4.1 java实现文件的读取:
InputStreamReader isr = new InputStreamReader(new FileInputStream("except.txt")); //用来读取文件中的数据 BufferedReader br = new BufferedReader(isr);//使用缓冲区,可以使用缓冲区的read(),readLine()方法; String s =null; s = br.readLine(); isr.close();//关闭文件
4.2 实现-c,-l,-w功能
InputStreamReader isr = new InputStreamReader(new FileInputStream(fileName)); //用来读取文件中的数据 BufferedReader br = new BufferedReader(isr);//使用缓冲区,可以使用缓冲区的read(),readLine()方法; while(br.read()!=-1)//read()=-1代表数据读取完毕 { String s = br.readLine(); if (s!=null) { countChar += s.length();//字符个数就是字符长度 countword += s.split(" |,").length;//split() 方法用于把一个字符串分割成字符串数组,字符串数组的长度,就是单词个数 if (isExcept){//如果有剔除表的话 String[] tmp=ExceptNameList(); String[] tmpS=s.split(" |,"); for (int i =0;ii){ for (int j=0;j j){ if (tmpS[i].equals(tmp[j])) countword--; } } } } countline++;//因为是按行读取,所以每次增加一即可计算出行的数目 } isr.close();//关闭文件 countChar++;
4.3 实现-s功能
File file = new File("G:\\java new projects\\WordCount"); String[] files = file.list(new FilenameFilter(){//得到该目录之下所有的.c文件 public boolean accept(File dir,String name){ return name.endsWith(".c"); } }); for(int i=0;i){
//文件名 输出文件名 是否统计字符数/单词数/行数 是否是全列表统计 是否使用停用表 Counts(files[i],outFileName,cs,ws,ls,isFileList,isExcept);//每调用一次这个函数就代表统计一次相应文件的字符数,单词数以及行数 }
4.4 实现-o功能
if (args[i].contains("-o")) isChangeOutFile=true;//标记是否使用其他输出文件 if (args[i].contains(".txt")&&isChangeOutFile)//得到输出文件名 outFileName="G:/java new projects/WordCount/"+args[i];
4.5实现-e功能
if (args[i].contains("-e")) isExcept=true;//使用停用表标记
//得到停用列表并分割开返回字符串数组 public static String[] ExceptNameList() throws IOException{ InputStreamReader isr = new InputStreamReader(new FileInputStream("except.txt")); //用来读取文件中的数据 BufferedReader br = new BufferedReader(isr);//使用缓冲区,可以使用缓冲区的read(),readLine()方法; String s =null; s = br.readLine(); isr.close();//关闭文件 return s.split(" "); }
if (isExcept){//如果有剔除表的话 String[] tmp=ExceptNameList();//停用表字符串 String[] tmpS=s.split(" |,");//统计的文件的字符串 for (int i =0;ii){ for (int j=0;j j){ if (tmpS[i].equals(tmp[j])) countword--; } } }
5.测试过程:
//测试字符数,单词数 public static void test1() { String[] args="wc.exe -c 1.c"; MainClass.start(args); } //测试单词数目 public static void test2() { String[]args="wc.exe -w 1.c"; MainClass.start(args); } //测试行数 public static void test3() { String[]args="wc.exe -l 1.c"; MainClass.start(args); } //测试输出文件 public static void test4() { String[]args="wc.exe -l -w -c out.txt -o 2.c"; MainClass.start(args); } //基本功能联合测试 public static void test5() { String[]args="wc.exe -l -c -w out.txt -o 2.c"; MainClass.start(args); } //测试处理目录下符合文件 public static void test6() { String[]args="wc.exe -l -c -w -s"; MainClass.start(args); } //测试停用词表 public static void test7() { String[]args="wc.exe -c -l -w 1.c -e"; MainClass.start(args); } //高级功能测试 public static void test8() { String[]args="wc.exe -l -c -w -s -o 2.c -e out.txt"; MainClass.start(args); }
6.参考文献连接:
http://blog.csdn.net/sunkun2013/article/details/13167099
https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/
https://zhidao.baidu.com/question/580312839.html