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

wordcount

时间:2017-09-21 23:27:08      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:ring   []   dem   generated   split   null   div   import   ini   

仔细看还是不太难的!

package demo2;

 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class wc {  
    public void wcexe() throws IOException{
        String filepath="C:/Users/Administrator/Desktop/123.txt";//文件路径
        BufferedReader br =null;
        int countWord=0;
        int countChar=0;
        int countLine=0;
        String s="";
        String strCount="";
        try {
             br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filepath))));
             while((s=br.readLine())!=null)
              {
                 s=s+" ";
                 strCount+=s;
               countLine++;
              }
             for(int i=0;i<strCount.split(" ").length;i++){
                 if(!strCount.split(" ")[i].equals(" "))
                     countWord++;
                 countChar+= strCount.split(" ")[i].length();
             }
             System.out.println("单词数:"+countWord);
             System.out.println("字符数:"+countChar);
             System.out.println("行数:"+countLine);
        catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            br.close();
        }
    }
    public static void main(String[] args) throws IOException{
        wc w=new wc();
        w.wcexe();
         
    }
}

wordcount

标签:ring   []   dem   generated   split   null   div   import   ini   

原文地址:http://www.cnblogs.com/7inkoo/p/7571769.html

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