個人小項目——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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM