WordCount(JAVA实现)
2024-10-19 02:14:31
201631103228,201631101227
1.项目需求
对程序设计语言源文件统计字符数、单词数、行数,统计结果以指定格式输出到默认文件中,以及其他扩展功能,并能够快速地处理多个文件。
wc.exe -c //返回文件的字符数
wc.exe -w //返回文件的单词总数
wc.exe -l //返回文件 的总行数,
wc.exe -o //将结果输出到指定文件OutPut.txt
2.开发环境 eclipse
3.开发语言 JAVA
4.项目地址
未打包
一、源码地址: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 {
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);
}
}
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.首先输入文件路径
2.文件OutPut.txt在D盘生成,并且将文件的字符数,单词数,行数写入
3.输入指定命令,获取相应的值
4.保存后的效果
最新文章
- maven如何配置
- Linux小技巧总结
- c#中两种不同的存储过程调用与比较
- Oracle备份及备份策略
- 第一章 zookeeper基础概念
- ABAP:区别CALL SCREEN/SET SCREEN/LEAVE TO SCREEN
- MVC 过滤器
- java 24点算法实现
- 6 款国外开源web oa办公系统(转)
- BZOJ1430: 小猴打架
- Handsontable 新增一行 默认值
- Android NDK进入发展
- 十六、Spring Boot 部署与服务配置
- exgcd学习笔记
- SqlServer中的系统数据库
- win10 搭建virtualenvwrapper虚拟环境
- windows下ngnix+php配置
- Oracle 基本语法、触发器、视图
- NIO 学习笔记三:DatagramChannel
- 一个tomcat下,两个系统的jar包可以相互引用。
热门文章
- Failed to start component [StandardEngine[Catalina].
- 【原】Coursera—Andrew Ng机器学习—Week 10 习题—大规模机器学习
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 9_Neural Networks learning
- #if (DEBUG)
- FreeSWITCH 客户端fs_cli连接设置(acl)
- js动态的给json对象添加新的元素
- Git,GitHub以及GitLab的区别
- linux系统命令行基本组成元素
- LA3983 捡垃圾的机器人
- Ubuntu14.04下使用PPA安装php5.6,php7