java实现WC项目
个人项目:WC
wc.exe 是一个常见的工具,它能统计文本文件的字符数、单词数和行数。这个项目要求写一个命令行程序,模仿已有wc.exe 的功能,并加以扩充,给出某程序设计语言源文件的字符数、单词数和行数。
GitHub地址:https://github.com/lllhhhyyy/mygit
基本功能列表
- -c [文件名] 返回文件的字符数(实现)
- -w [文件名] 返回文件词的数目(实现)
- -l [文件名] 返回文件的行数(实现)
扩展功能列表
- -s 递归处理目录下符合条件的文件。(实现)
- -a 返回更复杂的数据(代码行 / 空行 / 注释行)(实现)
- .处理通配符(未实现)
高级功能列表
1. -x显示图形界面(未实现)
PSP
PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 30 | 60 |
· Estimate | · 估计这个任务需要多少时间 | 240 | 300 |
Development | 开发 | 60 | 120 |
· Analysis | · 需求分析 (包括学习新技术) | 120 | 240 |
· Design Spec | · 生成设计文档 | 60 | 120 |
· Design Review | · 设计复审 (和同事审核设计文档) | 60 | |
· Coding Standard | · 代码规范 (为目前的开发制定合适的规范) | 0 | 0 |
· Design | · 具体设计 | 60 | 100 |
· Coding | · 具体编码 | 60 | 100 |
· Code Review | · 代码复审 | 20 | 20 |
· Test | · 测试(自我测试,修改代码,提交修改) | 60 | 120 |
Reporting | 报告 | 30 | 30 |
· Test Report | · 测试报告 | 30 | 30 |
· Size Measurement | · 计算工作量 | 20 | 30 |
· Postmortem & Process Improvement Plan | · 事后总结, 并提出过程改进计划 | 30 |
30 |
合计 | 880 |
1270 |
|
关键代码
package wc_lhy;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class Deal { public static void main(String []args) throws IOException{ HashMap<String, Boolean> orderMap = new HashMap<>();
String standar;
Pattern pattern;
Matcher matcher; orderMap.put("countLine", false);
orderMap.put("countChar", false);
orderMap.put("countWords", false);
orderMap.put("handleAll", false);
orderMap.put("complex", false); //校验格式
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
String order = scanner.nextLine();
//校验标准
standar = "^wc.exe\\s\\-[wacsl]{1,3}\\s.{0,}";
//匹配输入格式是否正确
pattern = Pattern.compile(standar);
matcher = pattern.matcher(order);
if(!matcher.matches()){
System.out.println("请输入正确的命令格式");
} //校验输入格式正确后,对输入命令按空格进行分割
String[] splitOrder = order.split("\\s+"); /*对输入命令分析需执行功能*/
String string = "\\-[wscal]";
//String path = splitOrder[splitOrder.length-1];
String path = splitOrder[splitOrder.length-1];
File file = new File(path);
for (int i = 1; i < splitOrder.length-1; i++) {
pattern = Pattern.compile(string);
Matcher matcher2 = pattern.matcher(splitOrder[i]);
if(matcher.matches()&&splitOrder[i].equals("-s")){
orderMap.put("handleAll", true);//处理所有文件
}
else if (matcher2.matches()&&splitOrder[i].equals("-w")) {
orderMap.put("countWords", true);//处理单词
}
else if(matcher2.matches()&&splitOrder[i].equals("-c")){
orderMap.put("countChar", true);//处理字符
}
else if(matcher2.matches()&&splitOrder[i].equals("-l")){
orderMap.put("countLine", true);
}
else if(matcher2.matches()&&splitOrder[i].equals("-a")){
orderMap.put("complex", true);//复杂功能
}
else {
System.out.println("出错!");
}
} Handle handle = new Handle(orderMap, path); //-s路径非文件夹
if(file.isDirectory()&&(orderMap.get("handleAll")==false)) System.out.println("非目录操作!请输入文件路径");
//-s操作+路径为文件夹
if (orderMap.get("handleAll")&&handle.isFolder()) {
handle.handleFoler(path);
}
//-s操作但路径不正确
else if((orderMap.get("handleAll") == true)&&(handle.isFolder() == false)){
System.out.println("请输入一个目录!");
}
//非-s操作且路径非文件夹啊
else if((orderMap.get("handleAll") == false)&&(handle.isFolder() == false)){
handle.judgeCount(path);
System.out.println("ok");
} }
}
-s功能,实现对目录所有文件的递归:
/*对文件夹进行的操作*/
public void handleFoler(String folderPath){
this.path = folderPath;
File file = new File(path);
//System.out.println("路径为"+file.getAbsolutePath());
File[] listFile = file.listFiles();
if(listFile==null){
return;
}
for(int i = 0; i< listFile.length; i++){
//非文件夹,即可进行对文件进行处理
if(!listFile[i].isDirectory()){
System.out.println("文件路径:"+listFile[i].getAbsolutePath());
System.out.println("文件名:"+listFile[i].getName());
try{
judgeCount(listFile[i].getAbsolutePath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
//为文件目录,递归处理
System.out.println("文件目录路径"+file.getAbsolutePath());
handleFoler(listFile[i].getAbsolutePath());
} }
}
-l 实现行数统计:
public void countLine(String path) throws IOException {
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(path));
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
while (bufferedReader.read()!=-1) {
String string = bufferedReader.readLine();
countLine++;//行数
}
bufferedReader.close();
System.out.println("行数: "+getCountLine());
}
-c 实现字符数统计:
public void countChar(String path) throws IOException {
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(path));
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
while (bufferedReader.read()!=-1) {
String string = bufferedReader.readLine();
if(string != null){
countChar = countChar + string.length();//字符个数
}
}
bufferedReader.close();
System.out.println("字符数: "+getCountChar());
}
-w 实现单词数统计:
public void countWords(String path) throws IOException {
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(path));
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
while (bufferedReader.read()!=-1) {
String string = bufferedReader.readLine(); if(string != null){
countWords = countWords + string.split("\\s").length;//词的个数
} }
bufferedReader.close();
System.out.println("单词数:" + getCountWords());
}
-a 统计代码行、空行、注释行:
//计算代码行,注释行。空白行
@SuppressWarnings("resource")
public void countComplex(String filePath) throws IOException {
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(filePath));
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
//注释行匹配
Pattern commetPattern = Pattern.compile("(^//.*$)|(^/\\*.*\\*/$)|(^/\\*\\*.*$)|(^\\*.*$)|.*\\*/$",
Pattern.MULTILINE + Pattern.DOTALL);
//空白行匹配
Pattern nullPattern = Pattern.compile("^\\s*$");
//代码行匹配
Pattern codePattern = Pattern.compile("\\s*",
Pattern.MULTILINE + Pattern.DOTALL);
//System.out.println(9);
String row;
while ((row = bufferedReader.readLine())!= null) {
if(nullPattern.matcher(row).find()){
countNull++;
}
//注释
if ((row!=null)&&commetPattern.matcher(row).matches()) {
commetLine++;
}
//代码行
if((row!=null)&&codePattern.matcher(row).find()) {
//else {
codeLine++;
}
}
codeLine = codeLine - commetLine-countNull;
}
代码测试:
测试文件:
运行结果:
测试文件:
运行结果:
测试文件:
项目总结:
完成了这个项目,感觉收获还是挺多的。首先是加强了自己对java编程的熟悉度,还有就是正则表达式,以前只是囫囵吞枣得匆匆看了一遍,实战是发现自己完全不会用,于是再去认真学习了一遍。在做这个项目的 过程中也是发现了自己存在的一些问题,首先就是算法不过关,考虑不全面,写的也不够精简,所以常常会抛空指针的错误,不过有错误还是挺好的,解决自己的错误才能更好的进步,在以后的编程中才会更加的小心。还有就是在刚开始时会觉得无从下手,特别是对正则表达式不熟悉,不知道要从哪里入手,因此开始编程浪费了挺多时间的。经过这次给了自己以后的一个小经验:即使问题很难,也要尝试着去动手。不要浪费时间去空想,着手,然后一个个的把这些问题解决掉。
java实现WC项目的更多相关文章
- 软件工程:java实现wc项目基本功能
项目相关要求 项目地址:https://github.com/xiawork/wcwork 实现一个统计程序,它能正确统计程序文件中的字符数.单词数.行数,以及还具备其他扩展功能,并能够快速地处理多个 ...
- 个人项目:Java实现WC
Java实现WC Github项目地址:https://github.com/auxshaw/WC 项目要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个 ...
- 小白のjava实现wc.exe功能
GitHub地址 项目完成情况 基本功能列表(已实现) wc.exe -c file.c //返回文件 file.c 的字符数 wc.exe -w file.c //返回文件 file. ...
- java实现wc
github项目传送门:https://github.com/yanghuipeng/wc 项目要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个命令行程 ...
- 软件工程第三个程序:“WC项目” —— 文件信息统计(Word Count ) 命令行程序
软件工程第三个程序:“WC项目” —— 文件信息统计(Word Count ) 命令行程序 格式:wc.exe [parameter][filename] 在[parameter]中,用户通过输入参数 ...
- 软工作业No.1。Java实现WC.exe
网址:https://github.com/a249970271/WC WC 项目要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个命令行程序,模仿已有w ...
- JAVA实现WC.exe功能
项目要求 实现一个统计程序,它能正确统计程序文件中的字符数.单词数.行数,以及还具备其他扩展功能,并能够快速地处理多个文件. 具体功能要求: 程序处理用户需求的模式为: wc.exe [paramet ...
- Java实现WC基本功能
GitHub仓库:https://github.com/douyazai/WCbase 一.WC 项目要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个命 ...
- java查看当前项目所有线程列表界面
java查看当前项目所有线程列表界面 1.TestThread(测试类) package com.isoftstone.pcis.isc.job.king.panel; public class Te ...
随机推荐
- python中迭代器(转)
一.迭代器与for语句 网上许多文章说Python的for语句中,in关键字后面的对象是一个集合.例如 for i in [1,2,3] print i 上面代码中in关键字后面的对象[1,2,3]是 ...
- jquery clone 获取文本框值得问题
1 clone 出来的文本框 默认不会把原来的事件也带过去 如果使用 $("#").clone(true); true 可以将原来的事件带过去 获取文本框的值 可以使用事件 ...
- php单点登陆简单实现 (iframe方式)
有四个网站分别为: www.a.com www.b.com www.c.com www.sso.com 需求是如果我们在sso登陆后,其他网站也会显示登陆中,不需要重复登陆,退出时,其他网站也会失效. ...
- PHP下ajax跨域的解决方案之CORS
由于安全的限制(同源策略,javascript只能访问同域名下的内容),如果需要进行跨域操作,那就免不了要进行跨域. CORS(跨域资源共享,Cross-Origin Resource Shari ...
- SqlDataHelper
using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using S ...
- mysql contact_ws函数 字符串被截断问题
contact函数默认有字符串长度限制,解决方法:SET group_concat_max_len = 20000这个参数设置一下就好了
- 前后台交互(打开前端页面,不传递任何数据,发送ajax请求)
1.打开前端,不传递任何数据 <script src="./jquery.min.js"></script> <script> $(docume ...
- inline 引起undefined reference to
main.cc:57: undefined reference to `evpp::udp::UdpDecoder::GetHeader()'collect2: error: ld returned ...
- Java读properties文件中文乱码问题的解决方法
java读properties文件,包含中文字符的主要有两种: 1.key中包含中文字符的(value中也有可能包含) 2.key中不包含中文字符的(value中有可能包含) 1.key中包含中文字符 ...
- 【Java】JavaWeb文件上传和下载
文件上传和下载在web应用中非常普遍,要在jsp环境中实现文件上传功能是非常容易的,因为网上有许多用java开发的文件上传组件,本文以commons-fileupload组件为例,为jsp应用添加文件 ...