java统计文件中字符,数字,汉字,空格数目

  1. import java.io.BufferedReader;  
  2. import java.io.File;  
  3. import java.io.FileNotFoundException;  
  4. import java.io.FileReader;  
  5. import java.util.regex.Matcher;  
  6. import java.util.regex.Pattern;  
  7.   
  8. /** 
  9.  *  java实现统计D:/aa.txt文件中出现的字母个数、数字个数、汉字个数、空格个数及行数 
  10.  * 
  11.  */  
  12. public class StatisticalCharacters {  
  13.   
  14.     /** 
  15.      * @param args 
  16.      * @throws FileNotFoundException  
  17.      */  
  18.     public static void main(String[] args) throws FileNotFoundException {  
  19.         String name = "D:/aa.txt"//文件名    
  20.         int num = 0;      //数字数    
  21.         int letter = 0;    //字母数    
  22.         int line = 0;    //行数    
  23.         int space = 0;  //空格数    
  24.         int word= 0;  //汉字数  
  25.         try{  
  26.           
  27.             File file=new File(name);  
  28.             BufferedReader br= new BufferedReader(new FileReader(file));  
  29.             String str = null;  
  30.               
  31.             while((str=br.readLine())!=null){  
  32.                 System.out.println(str);  
  33.                 line++;  
  34.                 num += countNumber(str);  
  35.                 letter += countLetter(str);  
  36.                 word += countChinese(str);  
  37.                 space += countSpace(str);  
  38.             }  
  39.           
  40.         }catch(Exception e){  
  41.             e.printStackTrace();  
  42.         }  
  43.   
  44.         System.out.println("数字数:"+num);  
  45.         System.out.println("字母数"+letter);  
  46.         System.out.println("汉字数"+word);  
  47.         System.out.println("空格数"+space);  
  48.         System.out.println("行数"+line);  
  49.     }  
  50.       
  51.     /** 
  52.      * 统计数字数 
  53.      * @param str 
  54.      * @return 
  55.      */  
  56.     public static int countNumber(String str) {  
  57.         int count = 0;  
  58.         Pattern p = Pattern.compile("\\d");  
  59.         Matcher m = p.matcher(str);  
  60.         while(m.find()){  
  61.             count++;  
  62.         }  
  63.         return count;  
  64.     }  
  65.   
  66.     /** 
  67.      * 统计字母数 
  68.      * @param str 
  69.      * @return 
  70.      */  
  71.     public static int countLetter(String str) {  
  72.         int count = 0;  
  73.         Pattern p = Pattern.compile("[a-zA-Z]");  
  74.         Matcher m = p.matcher(str);  
  75.         while(m.find()){  
  76.             count++;  
  77.         }  
  78.         return count;  
  79.     }  
  80.   
  81.     /** 
  82.      * 统计汉字数 
  83.      * @param str 
  84.      * @return 
  85.      */  
  86.     public static int countChinese(String str) {  
  87.         int count = 0;  
  88.         Pattern p = Pattern.compile("[\\u4e00-\\u9fa5]");  
  89.         Matcher m = p.matcher(str);  
  90.         while(m.find()){  
  91.             count++;  
  92.         }  
  93.         return count;  
  94.     }  
  95.       
  96.     /** 
  97.      * 统计空格数 
  98.      * @param str 
  99.      * @return 
  100.      */  
  101.     public static int countSpace(String str) {  
  102.         int count = 0;  
  103.         Pattern p = Pattern.compile("\\s");  
  104.         Matcher m = p.matcher(str);  
  105.         while(m.find()){  
  106.             count++;  
  107.         }  
  108.         return count;  
  109.     }  
  110.       
  111.   
  112. }  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值