第三周(JAVA编写的 wordcount)

 1 import java.io.*;
 2 public class WordCount {
 3     public static int words=1;
 4     public static int lines=1;
 5     public static int chars=1;
 6     public static void wc(InputStream f) throws IOException{
 7         int c=0;
 8         boolean last = false;
 9         String nullspace =" 	

";
10         
11         while ((c=f.read()) !=-1 ){
12             chars++;                    //包括空格  换行 
13             if (c == '
')
14             {
15                 lines++;
16             }
17             if (nullspace.indexOf(c)!=-1)
18             {
19                 if(last){
20                     words++;
21                 }
22                 last =false;
23             }
24             else{
25                 last=true;
26             }
27                 
28         }
29         
30     }
31     public static void main(String args[]){
32         FileInputStream f;
33         try{
34             if(args.length==0){
35                 f=new FileInputStream("h:/test.txt");
36                 wc(f);
37             }
38             
39                 
40         }catch (IOException e)  //确保程序不会因出错崩溃
41         {
42             System.out.println("文件不存在!");
43             return;
44         }
45         if(words==1 && lines==1 && chars==1)
46         {
47             System.out.println("文件为空!");
48         }
49         else{
50             System.out.println(lines + "行 
" + words + "个单词
" +  chars + "个字符");
51         }
52     }
53 
54 }
原文地址:https://www.cnblogs.com/Yililove/p/5301594.html