个人小项目——Java实现WC功能

  这个小项目用了两种方法解决了该功能的实现。

  1.两种方法的功能和具体实现

  代码可以成功运行,但是有一些情况考虑不完整,一种方法用了FileOutputStream输出流,为了解决空格无法统计问题,对文本实现一次重写,用String类的replace方法将空格用其他字符替换,然后可以实现字母数,单词数和行数的统计。另一种方法没有重新写文本,直接在缓冲区中处理文本,除上面三个之外还统计了空格数,字符总数和标点符号数。

  2.优缺点比较

  方法一可以统计出空行,而方法二由于是使用bufferedReader,每一行统计一次,所以无法读出没有内容的空行;但是方法二没有用到写入文本的FileOutputStream流,相对来说不容易出错。两种方法都有些小缺点,但能实现一般的统计功能。

  3.项目源码

  

  1 package pro2;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.File;
  5 import java.io.FileInputStream;
  6 import java.io.FileOutputStream;
  7 import java.io.IOException;
  8 import java.io.InputStreamReader;
  9 
 10 /**
 11  * 实现能计算一个文本的字符数,单词数和行数的功能
 12  * @author PC
 13  *
 14  */
 15 
 16 public class wcProject {
 17 
 18     private static int charcalculate=0; 
 19     private static int wordcalculate=0; 
 20     private static int linecalculate=0;
 21      
 22     //解析字符数,单词数,行数,空格数
 23     public static void calculate2() {
 24         String str="";
 25         int words = 0;//单词数
 26         int chars = 0;//字母数
 27         int lines = 0;//行数
 28         int spaces=0;//空格数
 29         int marks=0;//标点数
 30         int c=0;//字符数
 31         int t=0;//\t
 32         int count=0;
 33 
 34         FileInputStream fis=null;
 35         BufferedReader br=null;
 36         try {
 37             File file = new File("aaa.txt");
 38                 if (file.exists()){//判断文件是否存在
 39                     //打开文件输入流
 40                     fis=new FileInputStream(file);
 41                     //字符流写入了缓冲区
 42                     br=new BufferedReader(new InputStreamReader(fis));
 43                     while((str=br.readLine())!=null){//readLine()每次读取一行,转化为字符串,br.readLine()为null时,不执行
 44                         char[] b=str.toCharArray();//将字符串对象中的字符转换为一个字符数组
 45                         for (int i = 0; i < str.length(); i++) {
 46 //                            System.out.println("b[i]--"+b[i]);
 47                             if(b[i]!=' '&&b[i]!='\n'&&b[i]!='\t'&&b[i]!=','&&b[i]!='.'&&b[i]!='!'&&b[i]!=';'&&b[i]!='='){
 48                                 chars++;
 49                                 if(count>=1){
 50                                     count=0;
 51                                 }
 52                             }
 53                             if(b[i]==' '||b[i]=='\n'||b[i]=='\t'||b[i]==','||b[i]=='.'||b[i]=='!'||b[i]=='='||b[i]==';'){
 54                                 if(b[i]==' '){
 55                                     spaces++;
 56                                 }
 57                                 if(b[i]=='\t'){
 58                                     t++;
 59                                 }
 60                                 if (b[i]==','||b[i]=='.'||b[i]=='?'||b[i]=='!'||b[i]==';'){
 61                                     marks++;
 62                                 }
 63                                 
 64                                 words++;System.out.println("b[i]--"+b[i]+"--words--"+words);
 65                                 count++;
 66                                 if(count>1){
 67                                     words--;
 68                                 }
 69                             }
 70                         }
 71                         lines++;//行数(由于每次读取一行,行数自加即可)
 72                         c=chars+spaces+marks+t;
 73                     }
 74                     //关闭文件
 75                     br.close();
 76                     System.out.println("字符数:"+c+"单词数:"+(words+lines)+",字母数:"+chars+",行数:"+lines+",标点数:"+marks+",空格数:"+spaces);
 77                 }
 78             } catch (Exception e) {
 79                 e.printStackTrace();
 80             }
 81     }
 82     public static void calculate1() throws IOException
 83     {
 84         FileInputStream fis=new FileInputStream("aaa.txt");
 85         FileOutputStream fos=new FileOutputStream("bbb.txt");
 86             
 87         byte[] b=new byte[1024];
 88         int len=0;
 89         while((len=fis.read(b))!=-1){
 90             String str=new String(b,0,len); 
 91 //            System.out.println(str);
 92             String a=str.replace(" ",",");
 93             fos.write(a.getBytes());
 94         }    
 95             
 96         FileInputStream fis1=new FileInputStream("bbb.txt");
 97         int a;
 98         int count=0;
 99         while((a=fis1.read())!=-1){
100             if(a!=' '&&a!='\n'&&a!='\t'&&a!=','&&a!='.'&&a!='!'&&a!=';'&&a!='='){
101 //                System.out.println("c--"+(char)a);
102                 charcalculate++;
103                 if(count>=1){
104 //                    System.out.println("count--");
105                     count=0;
106                 }
107             }
108             if(a==' '||a=='\n'||a=='\t'||a==','||a=='.'||a=='!'||a=='='||a==';'){
109 //                   System.out.println("w--"+(char)a);
110                 wordcalculate++;
111                 count++;
112                 if(count>1){
113 //                    System.out.println("wordcalculate--");
114                     wordcalculate--;
115                 }
116             }
117             if(a=='\n'){
118 //                System.out.println("l--"+(char)a);
119                 linecalculate++;
120 //                count--;
121             }
122         }
123         charcalculate=charcalculate-linecalculate;    
124         linecalculate++;
125            
126         fis.close();
127         fos.close();
128             
129     }        
130     public static void main(String[] args) throws IOException{
131 //        calculate2();
132         
133 //        calculate1();
134 //        System.out.println("CharNum:"+charcalculate);
135 //        System.out.println("WordNum:"+wordcalculate);
136 //        System.out.println("LineNum:"+linecalculate);
137     }
138 
139 
140 }

本项目源码上传至个人GitHub:https://github.com/JingJiang0628/JavaLesson/blob/master/20170907-SoftwareEngineering/src/pro2/wcProject.java

 

posted @ 2017-09-18 13:08  Jing0628  阅读(1216)  评论(2编辑  收藏  举报