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 {
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.首先输入文件路径
2.文件OutPut.txt在D盘生成,并且将文件的字符数,单词数,行数写入
3.输入指定命令,获取相应的值
4.保存后的效果