最新WordConut
一、代码地址:https://gitee.com/cainiaoY/WordCount
二、项目分析:代码根据实现的功能不同分为两个模块,一个wcFuctiong类,一个wcTest类,其中wcFuction类实现计算txt文本中的内容的单词数、字符数和行数
wcText类实现根据输入命令的不同来实现wcFuction类中计算结果的展示
三、代码展示
import java.io.*;
import java.util.regex.*;
public class wcFuction {
private BufferedReader br;
//文件词统计函数
int getwordnumber(String filename) throws IOException {
int num=0;
String[] strword = null;
File file = new File(filename);
if(file.exists()) {
//读取文件
FileReader fr = new FileReader(filename);
br = new BufferedReader(fr);
String line = null;
StringBuffer sbf = new StringBuffer();
while((line=br.readLine())!= null) {
sbf.append(line);
String str = sbf.toString();
//正则表达式替换符号
str = str.replaceAll("[\\p{Nd}\\u9fa5-\\uffe5\\p{Punct}\\s&&[^-]]", " ");
//按空格将内容分割
strword = str.split("\\s+");
num=strword.length;
}
br.close();
fr.close();
}else {
System.out.println("文件不存在,请重新输入文件!");
}
return num;
}
//文件字符统计函数
int getCharacternumber(String filename) throws IOException {
int number = 0;
String[] strword = null;
File file = new File(filename);
if(file.exists()) {
//读取文件
FileReader fr = new FileReader(filename);
br = new BufferedReader(fr);
String line = null;
String str=null;
StringBuffer sbf = new StringBuffer();
while((line=br.readLine())!= null) {
sbf.append(line);
str = sbf.toString();
strword = str.split("\\s+");
}
for(int i=0;i<strword.length;i++) {
Pattern pattern = Pattern.compile("[0-9a-zA-Z]*");
Matcher matcher = pattern.matcher(strword[i]);
if(matcher.find()) {
number+=matcher.regionEnd();
}
}
br.close();
fr.close();
}else {
System.out.println("文件不存在,请重新输入文件!");
}
return number;
}
//文件行数统计函数
int getlinenumber(String filename) throws IOException {
int linenum = 0;
File file = new File(filename);
if(file.exists()) {
//读取文件
FileReader fr = new FileReader(filename);
//读取文件行数
LineNumberReader lnr = new LineNumberReader(fr);
while(lnr.readLine()!= null) {
linenum=lnr.getLineNumber();
}
lnr.close();
fr.close();
}else {
System.out.println("文件不存在,请重新输入文件!");
}
return linenum;
}
}
import java.io.IOException;
import java.util.Scanner;
public class wcTest
{
private static Scanner scanner;
public static void main(String[] args) throws IOException
{
String str = null;
wcFuction wcf = new wcFuction();
//循环询问命令输入
while(true)
{
System.out.print("请输入命令:");
//命令输入
scanner = new Scanner(System.in);
if(scanner.hasNext())
{
str=scanner.nextLine();
}
//分割命令,第一个作为判断第二个为文件路径
String[] strword = str.split(" ");
if(strword.length==2)
{
if(strword[0].equals("-c"))
{
int chara=wcf.getCharacternumber(strword[1]);
System.out.println("该文件的字符数:"+chara);
}
else if(strword[0].equals("-w"))
{
int word=wcf.getwordnumber(strword[1]);
System.out.println("该文件的词数:"+word);
}
else if(strword[0].equals("-l"))
{
int line=wcf.getlinenumber(strword[1]);
System.out.println("该文件的行数:"+line);
}
else
{
if(strword[0].equals("end"))
{
break;
}
else
{
System.out.println("命令输入错误,请重新输入!");
}
}
}
}
}
}
四、测试截图展示





五、实验总结
本次实验的结果基本与预期的结果一样,主要是在实现三个功能-------字符数上,行数,词数上花费的时间比较多。
最新WordConut的更多相关文章
- 终于等到你:CYQ.Data V5系列 (ORM数据层)最新版本开源了
前言: 不要问我框架为什么从收费授权转到免费开源,人生没有那么多为什么,这些年我开源的东西并不少,虽然这个是最核心的,看淡了就也没什么了. 群里的网友:太平说: 记得一年前你开源另一个项目的时候我就说 ...
- 最新的 cocoaPods 安装方法
经过努力终于发现了最新的 解决cocoaPods安装的办法: taobao Gems 源已停止维护,现由 ruby-china 提供镜像服务 第一步:安装rvm, 不管需不需要升级ruby,rvm可以 ...
- 最新Linux部署.NET,Mono and DNX
这几天一直在折腾在Linux下的ASP.NET 5,就下在看来在其它操作系统中ASP.NET 5或.NET应用,要想在完整的MS VM(CoreCLR)上运行还不远远达不到,应用的效果. 目前只能在M ...
- 吐血大奉献,打造cnblogs最新最火辣的css3模板(IE9以下请勿入内) -- 第一版
一直自己都想给自己的博客打造一个独一无二的皮肤,但是一直没有强劲的动力去完成这件事情.后来凭借着工作上面的需求(涉及到css3),就把自己的博客当成一个最好的试验场地.从而产生了你现在所看到的这个模板 ...
- CentOS 6.6 升级GCC G++ (当前最新版本为v6.1.0) (完整)
---恢复内容开始--- CentOS 6.6 升级GCC G++ (当前最新GCC/G++版本为v6.1.0) 没有便捷方式, yum update.... yum install 或者 添加y ...
- Oracle 11.2.0.4 RAC安装最新PSU补丁
环境:两节点RAC(RHEL 6.4 + GI 11.2.0.4 + Oracle 11.2.0.4) 需求:安装最新PSU补丁11.2.0.4.7 1.下载补丁和最新OPatch 2.检查数据库当前 ...
- 使用 NuGet 下载最新的 Rafy 框架及文档
为了让开发者更方便地使用 Rafy 领域实体框架,本月,我们已经把最新版本的 Rafy 框架程序集发布到了 nuget.org 上,同时,还把 RafySDK 的最新版本发布到了 VisualStud ...
- 利用TortoiseSVN获取最新版本的OpenCV源码
转自: http://blog.csdn.net/vsooda/article/details/7555969 1.下载安装TortoiseSVN:http://tortoisesvn.net/dow ...
- npm更新到最新版本的方法
打开命令行工具 npm -v 查看是否是最新版本 如果不是 运行npm i npm g 升级 打开C:\Users\用户名用户目录找到node_modules 文件夹下的npm文件夹,复制一份 打开n ...
随机推荐
- php字符串统计次数的各种方法(转)
<?php $str = 'AbCdEfGaBcDeFgH0234;,!-AaBbCcDdEeFfGg'; $str = strtoupper($str); // 不区分大小写时,全部转换成大写 ...
- Error: No EPCS layout data - looking for section [EPCS-C84018]
/********************************************************************** * Error: No EPCS layout data ...
- Chromium Settings页面修改
/********************************************************************** * Chromium Settings页面修改 * 说明 ...
- flask中自定义过滤器
第一种方法: 1,第一步:自定义过滤器函数 # 自定义一个函数,将list里面的数据进行排序 def list_sort(list) return list.sort() 2.第二步:注册过滤器 第一 ...
- linux局域网内挂载其它操作系统目录
一.linux挂载windows 1.windows目录打开共享: 2.mount -t cifs -o username=admin***tor,password=abc //192.168.*** ...
- go-json处理的问题
1.通过Decoder来解析json串 package main import ( "encoding/json" "fmt" "io" & ...
- 1_2_3_4_5 Html-Css
--------------------------------------------------------------------------------- HTML骨架 <HTML> ...
- PythonStudy——文件操作习题 Document operation exercises
# 1.统计文件数据中字母e出现的次数(不区分大小写)# 文件内容:hello friend, can you speak English!# 结果:4# 分析:将文件内容读出,然后统计读出的字符串中 ...
- 在IDEA中实战Git-branch
工作中多人使用版本控制软件协作开发,常见的应用场景归纳如下: 假设小组中有两个人,组长小张,组员小袁 场景一:小张创建项目并提交到远程Git仓库 场景二:小袁从远程Git仓库上获取项目源码 场景三:小 ...
- glog学习(一):glog的编译及demo
windows平台: 1.下载glog代码.下载地址:https://github.com/google/glog 2.使用cmake工具,获得对应的工程文件sln. 3.打开sln文件,生成对应的l ...