wc.java
1.项目相关要求
•基本功能列表:
-c 统计文件中字符的个数
-w 统计文件中的词数
-l 统计文件中的行数
•拓展功能:
-a 统计文件中代码行数、注释行数、空行
2.PSP2.1
|
PSP2.1 |
Personal Software Process Stages |
预估耗时(小时) |
实际耗时(小时) |
|
Planning |
计划 |
1.0 | 1.5 |
|
· Estimate |
· 估计这个任务需要多少时间 |
0.5 | 1 |
|
Development |
开发 |
30 | 40 |
|
· Analysis |
· 需求分析 (包括学习新技术) |
0.5 | 1 |
|
· Design Spec |
· 生成设计文档 |
0.5 | 0.6 |
|
· Design Review |
· 设计复审 (和同事审核设计文档) |
0.5 | 0.2 |
|
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
0.5 | 0.5 |
|
· Design |
· 具体设计 |
1 | 1.5 |
|
· Coding |
· 具体编码 |
30 | 40 |
|
· Code Review |
· 代码复审 |
1 | 2 |
|
· Test |
· 测试(自我测试,修改代码,提交修改) |
0.5 | 1 |
|
Reporting |
报告 |
0.5 | 0.5 |
|
· Test Report |
· 测试报告 |
0.5 | 0.5 |
|
· Size Measurement |
· 计算工作量 |
0.5 | 0.5 |
|
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
0.5 | 1 |
|
合计 |
39 | 52 |
3.解题思路
首先需要读取文件,然后通过命令 计算出字符、词、行、空行、注释行、代码行。
基础功能部分:读取文件后,将读取的文件转换成数组的形式,然后每生成一个数组,就是一行。其中,数组的长度就是字符数。利用正则表达式,判断词的开头结尾,进行计数。
拓展功能部分:利用正则表达式判断是否是注释行。数组长度为0或1为空行。然后代码行=总行数-空行-注释行
4.流程图

5.代码实现
字符数
public static void charNum()throws Exception
{ //文件导入
Scanner input = new Scanner(System.in);
System.out.println("please input path:");
String path = input.next();
int countChar = 0;
InputStreamReader isr = new InputStreamReader(new FileInputStream(path));
BufferedReader br = new BufferedReader(isr); while(br.read()!=-1)//read()=-1代表数据读取完毕
{
String s = br.readLine(); countChar += s.length();//字符个数就是字符长度
} isr.close();//关闭文件
System.out.println("字符个数 "+countChar); }
词数
public static void wordNum()throws Exception
{Scanner input = new Scanner(System.in);
System.out.println("please input path:");
String path = input.next();
int countword = 0;
InputStreamReader isr = new InputStreamReader(new FileInputStream(path));
BufferedReader br = new BufferedReader(isr);
while(br.read()!=-1)//read()=-1代表数据读取完毕
{
String s = br.readLine();
Pattern p = Pattern.compile("\\b[A-Za-z]+\\b");//创建以字母为开头或结尾的模板
Matcher m = p.matcher(s.toString());
while(m.find())
{
countword++;
} } isr.close();//关闭文件 System.out.println("单词个数 "+countword );
行数
public static void lineNum()throws Exception
{Scanner input = new Scanner(System.in);
System.out.println("please input path:");
String path = input.next();
int countline = 0;
int countcommentline = 0;
InputStreamReader isr = new InputStreamReader(new FileInputStream(path));
BufferedReader br = new BufferedReader(isr);
while(br.read()!=-1)//read()=-1代表数据读取完毕
{
String s = br.readLine();
countline++;//因为是按行读取,所以每次增加一即可计算出行的数目
}
isr.close();//关闭文件
System.out.println("行数 "+countline); }
空行,注释行,代码行
public static void alllineNum()throws Exception
{Scanner input = new Scanner(System.in);
System.out.println("please input path:");
String path = input.next();
int countline = 0;
int countcommentline = 0;
int countblankline = 0;
int codeNum=0;
InputStreamReader isr = new InputStreamReader(new FileInputStream(path));
BufferedReader br = new BufferedReader(isr);
while(br.read()!=-1)//read()=-1代表数据读取完毕
{
String s = br.readLine();
if(s.length()<=1)
{
countblankline++;
}
Pattern p = Pattern.compile("((/)|(/\\*+)|((^\\s)*\\*)|((^\\s)*\\*+/))+");//创建以注释符为开头或结尾的模板 Matcher m = p.matcher(s.toString());
while(m.find())
{
countcommentline++;
}
countline++;//因为是按行读取,所以每次增加一即可计算出行的数目
}
isr.close();//关闭文件
codeNum=countline-countcommentline-countblankline;
System.out.println("行数 "+countline);
System.out.println("注释行 "+countcommentline );
System.out.println("空行数 "+countblankline);
System.out.println("代码行 "+codeNum);
}
main函数
public static void main(String[] args) throws Exception
{ String bString = null;
System.out.println("请输入命令 ");
Scanner ms= new Scanner(System.in);
if (ms.hasNext()) {
bString = ms.next();
}
if (bString.equals("-c"))
{
s.charNum();
}
if (bString.equals("-w"))
{
s.wordNum();
}
if (bString.equals("-l"))
{
s.lineNum();
}
if (bString.equals("-b"))
{
s.blanklineNum(); }
if (bString.equals("-a"))
{
s.alllineNum(); } }
6.测试运行

7. 项目总结
通过这次项目,我发现自己知识体系有所欠缺,通过学习书本,和上网查询,请教同学得以解决。也学习到了许多新的知识。
wc.java的更多相关文章
- 个人项目-WC (java实现)
一.Github地址:https://github.com/734635746/WC 二.PSP表格 PSP2.1 Personal Software Process Stages 预估耗时(分钟) ...
- 个人项目 wc(java实现)
一.Github网址: https://github.com/Clarazhangbw/Wc.exe 二.PSP表 PSP2.1 Personal Software Process Stages 预估 ...
- WC Java 实现
项目 github 地址 一. 实现情况 基本要求 c 统计文件字符数 (实现) w 统计文件词数 (实现) l 统计文件行数(实现) 扩展功能 s 递归处理目录下符合条件得文件(实现) a 返回文件 ...
- 个人项目:Java实现WC
Java实现WC Github项目地址:https://github.com/auxshaw/WC 项目要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个 ...
- (https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014550004)Topic: Caught java.io.CharConversionException. ERRORCODE=-4220, SQLSTATE=null
270002WDPN 3 Posts 0 people l ...
- 本地idea开发mapreduce程序提交到远程hadoop集群执行
https://www.codetd.com/article/664330 https://blog.csdn.net/dream_an/article/details/84342770 通过idea ...
- HUST软测1504班第2周作业成绩:WordCount
说明 本次公布的成绩为第2周个人作业WordCount的结果: 第2周个人作业:WordCount 如果同学对作业结果存在异议,可以: 在毕博平台讨论区的第2周作业第在线答疑区发帖申诉. 或直接在博客 ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- 小白のjava实现wc.exe功能
GitHub地址 项目完成情况 基本功能列表(已实现) wc.exe -c file.c //返回文件 file.c 的字符数 wc.exe -w file.c //返回文件 file. ...
随机推荐
- hive 抽样方法
select * from (select *from advert.dws_advert_order_model_sample_pcvr_v2_diwhere dt>= date_sub('$ ...
- 【Java】JVM(一)、Java内存区域
一.程序计数器(Program Counter Register) 当前执行字节码的行号指示器,可以通过修改该计数器的值来实现字节码指令(分支,循环,跳转等), 每个线程都都有一个程序计数器, 属于线 ...
- Ubuntu下面网络固定ip
https://jingyan.baidu.com/article/e5c39bf5bbe0e739d7603396.html
- 跨域请求设置withCredentials
最近在做运动城项目,这一个项目下面有多个子项目,如主数据项目,pos项目等.主数据项目的域名为www.topmall.com,POS项目的域名为pos.topmall.com.即两个项目的主域名相同, ...
- 解决SpringBoot中webScocket不能注入bean的问题
最近在做websocket聊天,但是遇到一个问题,就是在websocket的服务中要调用Service层的东西.首先我想到的是通过@Autowire注解来实现bean的注入.但是进过测试发现,注入的b ...
- 几种TCP连接终止
在三次连接完成后,accept调用前,客户机发来RST. Berkeley实现将完全在内核中处理,不通知. 而SVR4实现将返回一个错误EPROTO,而POSIX指出应该是ECONNABORTED,后 ...
- 【Python基础教程第2版】——第二讲:列表和元组
引言: 什么是数据结构? 数据结果是通过某种方式(例如对元素进行编号)组织在一起的数据元素的集合.Python中最常用的数据结构是序列. Python包含6种内建的序列:列表和元组(最常用:列表可以修 ...
- [leetcode]211. Add and Search Word - Data structure design添加查找单词 - 数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- php中session入memcached
1.使用memcache扩展,提供的session处理器(session.save_handler) memcahe即可. 通过存储位置配置项(session.save_path),设置使用的memc ...
- VC++ 定时器使用总结
VC++ WM_TIMER 定时器使用方法 在编程时,会经常使用到定时器.使用定时器的方法比较简单,通常告诉Windows一个时间间隔,然后WINDOWS以此时间间隔周期性触发程 ...