java wordcount_WordCount(JAVA实现)

201631103228,201631101227

1.项目需求

对程序设计语言源文件统计字符数、单词数、行数,统计结果以指定格式输出到默认文件中,以及其他扩展功能,并能够快速地处理多个文件。

wc.exe -c     //返回文件的字符数

wc.exe -w      //返回文件的单词总数

wc.exe -l     //返回文件 的总行数,

wc.exe -o      //将结果输出到指定文件OutPut.txt

2.开发环境  eclipse

3.开发语言  JAVA

4.项目地址

9295be24683a16153523ce00f3c0479c.png

未打包

一、源码地址:https://gitee.com/shuaiqin/WordCount

二、源码

package readTXT;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.util.Scanner;

public class ReadTxt {

public static void main(String[] args) throws Exception {

Scanner input = new Scanner(System.in);

System.out.println("请输入文件路径:");

String path = input.next();

int countChar = 0;

int countword = 0;

int countline = 0;

InputStreamReader isr = new InputStreamReader(new FileInputStream(path));

//用来读取文件中的数据

BufferedReader br = new BufferedReader(isr);//使用缓冲区,缓存输入的文档,可以使用缓冲区的read(),readLine()方法;

while(br.read()!=-1)//read()=-1代表数据读取完毕

{

String s = br.readLine();

countChar += s.length();//字符个数就是字符长度

countword += s.split(" ").length;//split() 方法用于把一个字符串分割成字符串数组,字符串数组的长度,就是单词个数//在此只适用于英文

countline++;//因为是按行读取,所以每次增加一即可计算出行的数目

}

isr.close();//关闭文件

//新建一个txt文件

File fp=new File("D:\\OutPut.txt");

String strchar=Integer.toString(countChar);

String strword=Integer.toString(countword);

String strline=Integer.toString(countline);

PrintWriter pfp=new PrintWriter(fp);

pfp.println("字符数为:"+strchar);

pfp.println("单词数为:"+strword);

pfp.println("行数为:"+strline);

pfp.close();

System.out.println("OutPut.txt已经保存在D盘下");

System.out.println("请输入字符命令:(-c查询字符数,-w查询单词数,-o查询行数)");

String p=input.next();

if(p.equals("-c"))

{

System.out.println("字符数:"+countChar);

}else

if(p.equals("-w"))

{

System.out.println("单词数:"+countword );

}

else

if(p.equals("-o"))

System.out.println("行数"+countline);

}

}

三、测试

1.首先输入文件路径

2b0f53c6c4e8f1da9223e6716279d3de.png

2.文件OutPut.txt在D盘生成,并且将文件的字符数,单词数,行数写入

997fab9ab1e15fa8d66ab63ced48a0a6.png

3.输入指定命令,获取相应的值

bebc1a33e6e48234837c64f7756ca33c.png

4.保存后的效果

85b031771e388890e0bef402e819a7fb.png

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值