一、代码地址: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. brand new start

    做了约两年半的安全,留下了约五十多篇笔记,从电脑搬过来,免的丢了

  2. 【leetcode】437. Path Sum III

    problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完

  3. OpenCV中的图像插值示例

    本文地址:http://www.cnblogs.com/QingHuan/p/7384433.html,转载请注明出处 ======================================== ...

  4. mysql数据库-定义函数-存储过程写法

    ------------- mysql  定义自定义函数写法 DELIMITER $$ USE `iwmsdb`$$ DROP FUNCTION IF EXISTS `F_WM_DBNAME`$$ C ...

  5. Day 10 函数的形参,实参

    今日内容 '''实参:调用函数,在括号内传入的实际值,值可以为常量.变量.表达式或三者的组合​*****形参:定义函数,在括号内声明的变量名,用来接受外界传来的值​'''​'''注:形参随着函数的调用 ...

  6. 8、sort排序中比较函数的几种应用方式

    1.待排序中的元素作数组的下标或map的键值 例题:PAT甲级_1141 PAT Ranking of Institutions #include<bits/stdc++.h> using ...

  7. python eval()和exec()以及complie()

    1.eval() 函数 eval() 函数用来执行一个字符串表达式,并返回表达式的值. ------->>  eval(expression[, globals[, locals]]) 参 ...

  8. selenium +chromdriver模块

    1   selenium 模拟浏览器行为 2 chromdriver   对应的chrome浏览器驱动 下载地址 注意:chrome与chromdriver存在对应关系   以下错误就可能是版本不对应 ...

  9. [UE4]Image

    一.Image.Appearance.Brush.Tiling:平铺方式 1.No Tile:不平铺,拉伸会变形 2.Horizontal:横向平铺.纵向拉伸会变形 3.Vertical:纵向平铺.横 ...

  10. iOS开发SDWebImageOptions理解

    iOS开发SDWebImageOptions理解 原文 http://www.cnblogs.com/WJJ-Dream/p/5816750.html typedef NS_OPTIONS(NSUIn ...