最新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 ...
随机推荐
- python中datetime常用方法
# 可运算的时间方法包 >>> import datetime >>> import time >>> res = datetime.dateti ...
- hdu 1166 (单点更新+区间求和+裸题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissi ...
- Arch Linux root密码忘记了怎么办
https://wiki.archlinux.org/index.php/Reset_root_password_(简体中文)https://wiki.archlinux.org/index.php/ ...
- rviz初接触
- hsdfz -- 6.17 -- day2
今日依旧康复…… 当天晚上被老师拉去小吃街了,晚上回来精力憔悴,所以并没有当天写 反正就惨,因为估错复杂度,期望得分100分最后结果20分 (我的复杂度是nlog^2n的,正确性有保障,稳! 事后:还 ...
- PythonStudy——字典的定义 Dictionary definition
# 空字典 d1 = {} d2 = dict() # 用map映射创建字典 d3 = dict({'a': 1, 'b': 1}) print(d3) # 用关键字赋值方式 d4 = dict(na ...
- [转]python中pandas库中DataFrame对行和列的操作使用方法
转自:http://blog.csdn.net/u011089523/article/details/60341016 用pandas中的DataFrame时选取行或列: import numpy a ...
- 解压zipfile & tarfile
def __un_zip(self, file_path): """解压.zip格式文件到同名目录下,若解压之前就存在该目录说明已解压,跳过解压过程,返回该目录" ...
- mysqli字符编码
mysqli 字符编码: 汉字编码: 1.gbk 最久的编码格式,不能写繁体: 2.国内的gb2312: 3.国际的标准:utf-8; 查看数据库的字符编码: show variables like ...
- Scrapy学习篇(九)之文件与图片下载
Media Pipeline Scrapy为下载item中包含的文件(比如在爬取到产品时,同时也想保存对应的图片)提供了一个可重用的 item pipelines . 这些pipeline有些共同的方 ...