zoukankan      html  css  js  c++  java
  • java实现wc.exe的基本功能

       版权声明:本文为博主原创文章,转载请声明。

      今天用java实现了wc.exe的基本功能,感觉还是蛮简单的,重点是读取字符串时候,空格也会读取进去,如果不处理一下的话,空格也会算进字符里面.。

      需要注意的是,如果是中文的话,java会算两个两个字节读取。所以这个代码只能统计英文。

      github地址:https://github.com/ICanV/wc

      代码如下:

    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();
    		
    	}
    }
    

      

      

  • 相关阅读:
    [ APIO 2015 ] 雅加达的摩天楼
    「POI2011 R1」Conspiracy
    「COCI2016/2017 Contest #2」Bruza
    「THUWC 2017」随机二分图
    「HAOI2015」按位或
    Topcoder Srm 726 Div1 Hard
    「LOJ6482」LJJ爱数数
    「2017 山东一轮集训 Day4」基因
    「Codechef April Lunchtime 2015」Palindromeness
    「UOJ207」共价大爷游长沙
  • 原文地址:https://www.cnblogs.com/liupeixuan/p/7535078.html
Copyright © 2011-2022 走看看