码迷,mamicode.com
首页 > 其他好文 > 详细

Wordcount

时间:2017-09-20 14:27:24      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:row   字节   直接   数组   rgs   一个   lin   har   count   

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.util.Scanner;
/*
* InputStreamReader(new FileInputStream(绝对文件名))进行文件的读取
* BufferedReader(文件读取)调用readLine()的方法
*/
public class wordcount {
public static void main(String[] args) throws Exception {
// 统计一个文件的字符数,单词数,行数
Scanner input = new Scanner(System.in);
System.out.println("please input path:");
String path = input.next();
int countChar = 0;
int countword = 0;
int countline = 0;
InputStreamReader isr = new InputStreamReader(new FileInputStream(path));
//InputStreamReader将字符流向字节流转换。
//InputStreamReader isr = new InputStreamReader(new FileInputStream(绝对文件名));
//用来读取文件中的数据
BufferedReader br = new BufferedReader(isr);//使用缓冲区,可以使用缓冲区的read(),readLine()方法;
/*readLine每次读取一行,read()读取整个文件,是生成文件内容最直接的方式,如果连续面向行的处理则是没有必要的
可直接综合为
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
*/
while(br.read()!=-1)//read()=-1代表数据读取完毕
{
String s = br.readLine();
countChar += s.length();//字符个数就是字符长度
countword += s.split(" ").length;//split() 方法用于把一个字符串分割成字符串数组,字符串数组的长度,就是单词个数
countline++;//因为是按行读取,所以每次增加一即可计算出行的数目
}
isr.close();//关闭文件
System.out.println("char cont "+countChar);
System.out.println("word count "+countword );
System.out.println("line count "+countline);
}
}

 

[代码出自CSDN博主(洋葱ycy)]

Wordcount

标签:row   字节   直接   数组   rgs   一个   lin   har   count   

原文地址:http://www.cnblogs.com/QQ1210611769/p/7560482.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!