统计txt文件汉字个数

这里需需要注意一下:

如果txt文件的编码不是utf-8会是乱码,所以需要设置一下txt的编码。

 

package com.java.hanzi.utf;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TxtCount {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File file = new File("D:\\2012-0414.txt");
		try {
			//FileInputStream fin = new FileInputStream(file);
			//FileReader默认使用的是GBK,查看123.txt文件的编码格式
		//	FileInputStreamReader(new InputStreamReader(new FileInputStream("path")),"UTF-8")
			//
			int count = 0;
		//	FileReader fr = new FileReader(file);
			//System.out.println("fr.getEncoding()="+fr.getEncoding());
		//	BufferedReader bf = new BufferedReader(fr);
			BufferedReader br=new BufferedReader(new InputStreamReader(new  FileInputStream(file),"UTF-8")); 
		//	System.out.println("fr.getEncoding()="+fr.getEncoding());
			String str = null;
			while((str=br.readLine())!=null){
				count = count + calculator(str);
			}
			System.out.println("-----"+count);
			br.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static int calculator(String str){
			int count = 0;
	        String regEx = "[\\u4e00-\\u9fa5]";
	        Pattern p = Pattern.compile(regEx);
	        Matcher m = p.matcher(str);
	        while (m.find()) {
	            for (int i = 0; i <= m.groupCount(); i++) {
	                count = count + 1;
	            }
	        }
	        System.out.println(str);
	        System.out.println("共有 " + count + "个 ");
	        return count;
	}

}

转载于:https://my.oschina.net/winHerson/blog/53751

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值