这个小项目用了两种方法解决了该功能的实现。

  1.两种方法的功能和具体实现

  代码可以成功运行,但是有一些情况考虑不完整,一种方法用了FileOutputStream输出流,为了解决空格无法统计问题,对文本实现一次重写,用String类的replace方法将空格用其他字符替换,然后可以实现字母数,单词数和行数的统计。另一种方法没有重新写文本,直接在缓冲区中处理文本,除上面三个之外还统计了空格数,字符总数和标点符号数。

  2.优缺点比较

  方法一可以统计出空行,而方法二由于是使用bufferedReader,每一行统计一次,所以无法读出没有内容的空行;但是方法二没有用到写入文本的FileOutputStream流,相对来说不容易出错。两种方法都有些小缺点,但能实现一般的统计功能。

  3.项目源码

  

 package pro2;

 import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader; /**
* 实现能计算一个文本的字符数,单词数和行数的功能
* @author PC
*
*/ public class wcProject { private static int charcalculate=0;
private static int wordcalculate=0;
private static int linecalculate=0; //解析字符数,单词数,行数,空格数
public static void calculate2() {
String str="";
int words = 0;//单词数
int chars = 0;//字母数
int lines = 0;//行数
int spaces=0;//空格数
int marks=0;//标点数
int c=0;//字符数
int t=0;//\t
int count=0; FileInputStream fis=null;
BufferedReader br=null;
try {
File file = new File("aaa.txt");
if (file.exists()){//判断文件是否存在
//打开文件输入流
fis=new FileInputStream(file);
//字符流写入了缓冲区
br=new BufferedReader(new InputStreamReader(fis));
while((str=br.readLine())!=null){//readLine()每次读取一行,转化为字符串,br.readLine()为null时,不执行
char[] b=str.toCharArray();//将字符串对象中的字符转换为一个字符数组
for (int i = 0; i < str.length(); i++) {
// System.out.println("b[i]--"+b[i]);
if(b[i]!=' '&&b[i]!='\n'&&b[i]!='\t'&&b[i]!=','&&b[i]!='.'&&b[i]!='!'&&b[i]!=';'&&b[i]!='='){
chars++;
if(count>=1){
count=0;
}
}
if(b[i]==' '||b[i]=='\n'||b[i]=='\t'||b[i]==','||b[i]=='.'||b[i]=='!'||b[i]=='='||b[i]==';'){
if(b[i]==' '){
spaces++;
}
if(b[i]=='\t'){
t++;
}
if (b[i]==','||b[i]=='.'||b[i]=='?'||b[i]=='!'||b[i]==';'){
marks++;
} words++;System.out.println("b[i]--"+b[i]+"--words--"+words);
count++;
if(count>1){
words--;
}
}
}
lines++;//行数(由于每次读取一行,行数自加即可)
c=chars+spaces+marks+t;
}
//关闭文件
br.close();
System.out.println("字符数:"+c+"单词数:"+(words+lines)+",字母数:"+chars+",行数:"+lines+",标点数:"+marks+",空格数:"+spaces);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void calculate1() throws IOException
{
FileInputStream fis=new FileInputStream("aaa.txt");
FileOutputStream fos=new FileOutputStream("bbb.txt"); byte[] b=new byte[1024];
int len=0;
while((len=fis.read(b))!=-1){
String str=new String(b,0,len);
// System.out.println(str);
String a=str.replace(" ",",");
fos.write(a.getBytes());
} FileInputStream fis1=new FileInputStream("bbb.txt");
int a;
int count=0;
while((a=fis1.read())!=-1){
if(a!=' '&&a!='\n'&&a!='\t'&&a!=','&&a!='.'&&a!='!'&&a!=';'&&a!='='){
// System.out.println("c--"+(char)a);
charcalculate++;
if(count>=1){
// System.out.println("count--");
count=0;
}
}
if(a==' '||a=='\n'||a=='\t'||a==','||a=='.'||a=='!'||a=='='||a==';'){
// System.out.println("w--"+(char)a);
wordcalculate++;
count++;
if(count>1){
// System.out.println("wordcalculate--");
wordcalculate--;
}
}
if(a=='\n'){
// System.out.println("l--"+(char)a);
linecalculate++;
// count--;
}
}
charcalculate=charcalculate-linecalculate;
linecalculate++; fis.close();
fos.close(); }
public static void main(String[] args) throws IOException{
// calculate2(); // calculate1();
// System.out.println("CharNum:"+charcalculate);
// System.out.println("WordNum:"+wordcalculate);
// System.out.println("LineNum:"+linecalculate);
} }

本项目源码上传至个人GitHub:https://github.com/JingJiang0628/JavaLesson/blob/master/20170907-SoftwareEngineering/src/pro2/wcProject.java

个人小项目——Java实现WC功能的更多相关文章

  1. java实现wc功能

    github项目地址:https://github.com/3216004717/ruanjiangongcheng.git 项目相关要求 基本要求 wc.exe -c file.c //返回文件 f ...

  2. 软件工程实践小项目之模拟wc.exe的小程序

    github源码和工程文件地址:https://github.com/Jackchenyu/Word_counts/tree/smart 基本要求:要实现wc的基本功能即文件中字符数.单词数.行数的统 ...

  3. 模拟XShell的小项目

    不知道大家有没有用过XShell这款工具,这款工具通过windows可以远程操作处于开机状态的linux操作系统,也就是说把你的电脑和一台服务器连入网络,你通过输入服务器所在的IP地址建立一个会话就可 ...

  4. iOS:quartz2D绘图小项目(涂鸦画板)

    介绍:学了quartz2D的绘图知识后,我根据它的一些功能制作了一个小项目:涂鸦画板. 功能:绘制各种图形,还可以选取相册上的照片做涂鸦,然后保存到相册中.其中,还包括功能有:颜色的选取.线宽的选取. ...

  5. Java小项目--坦克大战(version1.0)

    Java小项目--坦克大战<TankWar1.0> 这个小项目主要是练习j2se的基础内容和面向对象的思想.项目实现了基本的简单功能,我方一辆坦克,用上下左右键控制移动方向,按F键为发射炮 ...

  6. 小白のjava实现wc.exe功能

    GitHub地址 项目完成情况 基本功能列表(已实现) wc.exe -c file.c     //返回文件 file.c 的字符数 wc.exe -w file.c    //返回文件 file. ...

  7. 个人项目:Java实现WC

    Java实现WC Github项目地址:https://github.com/auxshaw/WC 项目要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个 ...

  8. JAVA实现WC.exe功能

    项目要求 实现一个统计程序,它能正确统计程序文件中的字符数.单词数.行数,以及还具备其他扩展功能,并能够快速地处理多个文件. 具体功能要求: 程序处理用户需求的模式为: wc.exe [paramet ...

  9. 个人项目:实现wc.exe(Java)

    本项目Github地址:https://github.com/NNewBoy/wc 项目相关要求 基本功能:(已实现) -c 统计文件字符数 -w 统计文件词的数目 -l 统计文件行数 扩展功能:(已 ...

随机推荐

  1. Web安全学习笔记——SQL注入

    一.MySQL注入 1. 常用信息查询 常用信息: 当前数据库名称:database() 当前用户:user() current_user() system_user() 当前数据库版本号:@@ver ...

  2. 剑指offer——面试题32:从上到下打印二叉树

    void BFS(BinaryTreeNode* pRoot) { if(pRoot==nullptr) { cout<<"empty binary tree!"< ...

  3. OpenERP 干掉 产品计量单位中的 search more 和 create and edit

    实际操作中特别容易点错而新建了重复的单位,通过下边的方法可以将“search more”和 “create and edit”干掉 在新继承product.product的模块中,修改xml文件 这样 ...

  4. Mac下锁屏快捷键

    ▲先设置“进入眨眼或开始屏幕保护程序”选择”立即“要求输入密码. [系统编好设置]->[通用] ▲通过快捷键[shift+control+光驱键]或者[option+command+关机键]

  5. 开源一个Java Class实现Openfire登陆、推出、消息发送,方便其他系统集成IM功能了

    开源一个Java Class实现Openfire登陆.推出.消息发送 N年前写的,希望对Openfire开发新手有帮助哦 import java.util.*; import java.io.*;   ...

  6. Python离线安装依赖包

    1.制作requirement.txt pip freeze > requirement.txt 2.下载离线Pytho安装包 pip download -r requirement.txt - ...

  7. the unchecked warnings

    5.1.9. Unchecked Conversion Let G name a generic type declaration with n type parameters. There is a ...

  8. MySQL数据库-错误1166 - Incorrect column name 'xxx' 的解决方法

    在用Navicat for MySQL给MySQL数据库修改表的字段时报如下的错误: 解决方法:检查字段里面是不是有空格,去掉就可以了.

  9. 6.046 Design and Analysis of Algorithms

    课程信息 6.046 Design and Analysis of Algorithms

  10. js判断向量叉点 并求出交点坐标

     代码如下可以直接运行,判断向量相交并求出交点坐标 <!DOCTYPE html> <html> <head> <meta http-equiv=" ...