StreamTokenizer (流标记) 示例

字符,ttype包含该字符的值。如果遇到一个行结束情况,ttype等于TT_EOL(这假定了参数为true调用eolIsSignificant())。如果遇到流的结尾,ttype 等于TT_EOF。

*/

//Enhanced word count program that uses a StreamTokenizer.
import java.io.*;
class STWordCount{
public static int intWords = 0;
public static int intLines = 0;
public static int intChars = 0;
public static void wc(Reader r) throws IOException{
   StreamTokenizer tok = new StreamTokenizer(r);
   tok.resetSyntax();
   tok.wordChars(33,255);
   tok.whitespaceChars(0,' ');
   tok.eolIsSignificant(true);
   while(tok.nextToken() != tok.TT_EOF){
    switch(tok.ttype){
     case tok.TT_EOL: //不知道为什么会报错
      intLines++;
        intChars++;
      break;
     case tok.TT_WORD: //不知道为什么会报错
      intWords++;
     default:
      intChars += tok.sval.length();
    }
   }
}
public static void main(String[] args)
{
   if(args.length == 0){
    try{
     wc(new InputStreamReader(System.in));
     System.out.println(intLines + " " + intWords + " " + intChars);
    }catch(IOException e){
    
    }
   }else{
    int tWords = 0,tLines = 0,tChars = 0;
    for(int i =0;i<args.length;i++){
     try{
      intWords = intLines = intChars = 0;
      wc(new FileReader(args[i]));
      tWords += intWords;
      tLines += intLines;
      tChars += intChars;
      System.out.println(args[i] + ": " +intLines+ " " + intWords + " " + intChars);
     }catch(IOException e){
      System.out.println(args[i] +":error.");
     }
    }
    System.out.println("Total :" +tLines+ " " + tWords + " " + tChars);
   }
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值