一、代码地址:https://gitee.com/cainiaoY/WordCount

二、代码:

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的更多相关文章

  1. 最新WordConut

    一.代码地址:https://gitee.com/cainiaoY/WordCount 二.项目分析:代码根据实现的功能不同分为两个模块,一个wcFuctiong类,一个wcTest类,其中wcFuc ...

  2. [Hive_add_6] Hive 实现 Word Count

    0. 说明 Hive 通过 explode()函数 和 split()函数 实现 WordConut 1. Hive 实现 Word Count 方式一 1.1 思路 将每一行文本变为 Array 数 ...

  3. 学习笔记—MapReduce

    MapReduce是什么 MapReduce是一种分布式计算编程框架,是Hadoop主要组成部分之一,可以让用户专注于编写核心逻辑代码,最后以高可靠.高容错的方式在大型集群上并行处理大量数据. Map ...

  4. 用IDEA编写spark的WordCount

    我习惯用Maven项目 所以用IDEA新建一个Maven项目 下面是pom文件 我粘上来吧 <?xml version="1.0" encoding="UTF-8& ...

  5. Storm之WordCount初探

    刚接触Strom,记录下执行过程 1.pom.xml <?xml version="1.0" encoding="UTF-8"?> <proj ...

随机推荐

  1. ubuntu16.04下 sublime text输入中文

    1.git clone https://github.com/lyfeyaj/sublime-text-imfix.git 2.cd sublime-text-imfix && ./s ...

  2. 各机器学习方法代码(OpenCV2)

    #include <iostream> #include <math.h> #include <string> #include "cv.h" ...

  3. Centos7安装mysql5.6.29shell脚本

    创建脚本mysql.sh,直接运行sh mysql.sh #!/bin/bash if [ -d /software ] ;then cd /software else mkdir /software ...

  4. varchar字数

    每行数据最多65000字节 长度是当前字符集的字符长度,而不是字节长度! 参考:https://www.cnblogs.com/billyxp/p/3548540.html 经常变化的字段用varch ...

  5. Spring Cloud(Dalston.SR5)--Zuul 网关-路由配置

    Spring Cloud 在 Zuul 的 routing 阶段实现了几个过滤器,这些过滤器决定如何进行路由工作. 简单路由(SimpleHostRoutingFilter) 该过滤器运行后,会将 H ...

  6. 后台自动运行,定期记录定位数据(Hbuilder监听 app由前台切换到后台、切换运行环境的 监听方法)

    http://ask.dcloud.net.cn/question/28090 https://blog.csdn.net/qq_37508970/article/details/86649703 各 ...

  7. 1、minimum-depth-of-binary-tree

    题目描述 Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the ...

  8. Java构造器练习题

    仔细阅读下面的程序 public class Car { String name = "汽车"; public Car(String name) { this.name = nam ...

  9. nginx配置分发Tomcat服务,负载均衡

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 项目中瓦片资源越来越多,如果提高瓦片的访问效率是一个需要解决的 ...

  10. JVM内部细节之一:synchronized关键字及实现细节(轻量级锁Lightweight Locking)

    在C程序代码中我们可以利用操作系统提供的互斥锁来实现同步块的互斥访问及线程的阻塞及唤醒等工作.然而在Java中除了提供Lock API外还在语法层面上提供了synchronized关键字来实现互斥同步 ...