properties语言包字数统计

今天需要做个预算,需要统计语言包中要翻译的中文文字数,文字有汉字和Unicode码,想了想还是动动手写点代码来统计。

先取一下目录下所有的文件。扔到Excel里。

这里写图片描述

然后运行下列代码:
注意修改final HSSFSheet sheet = wb.getSheetAt(4); 中的sheet页:4取的是第5个sheet。
System.out.println(a);会输出文件的字数。复制到sheet中方便统计。

代码如下:
package com.hoperun.eesite.modules.listener;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.hoperun.eesite.common.utils.StringUtils;

public class getContentSize {
public static void main(String[]args) throws FileNotFoundException, IOException{  

final File file = new File("E:\\Project\\SD-WAN\\语言包统计.xls");


final ArrayList<ArrayList<Object>> rowList = new ArrayList<ArrayList<Object>>();
ArrayList<Object> colList;
final HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(file));
final HSSFSheet sheet = wb.getSheetAt(4);
HSSFRow row;
HSSFCell cell;
   // for (int i = sheet.getFirstRowNum(), rowCount = 0; rowCount < sheet.getPhysicalNumberOfRows(); i++)
   for (int i = sheet.getFirstRowNum(), rowCount = 0; rowCount < 192; i++)
    {
    row = sheet.getRow(i);
    colList = new ArrayList<Object>();
    if (row == null)
    {
        //当读取行为空时
        if (i != sheet.getPhysicalNumberOfRows())
        {//判断是否是最后一行
            rowList.add(colList);
        }
        continue;
    }
    else
    {
        rowCount++;
    }


    cell = row.getCell(1);
    String fileNamde = cell.getStringCellValue();
    int a =  getContent(fileNamde);
   System.out.println(a);

} 

}


public static int getContent(String path) throws IOException{

    //获取文件对应的BufferedReader
    BufferedReader br = new BufferedReader(new FileReader(new File(path)));

    String tempstr; //临时字符串
    int num_of_words = 0; //总汉字数
    //int num_of_wordsAndPunctuation = 0; //汉字+标点
    //int num_blank = 0; //空格字符

    int linenull = 0;

    Pattern pattern =  Pattern.compile("([\u4e00-\u9fa5]{1})"); //定义匹配模式:1个汉字
   // Pattern pattern2 = Pattern.compile("([\u4e00-\u9fa5,,.。、/<>??;;'‘’:\"【】{}]{1})"); //定义匹配模式:汉字或标点符号
   // Pattern pattern3 = Pattern.compile("[\\s]");


   // while((tempstr = br.readLine()) != null && tempstr != ""){
    while( linenull< 5){
        tempstr = br.readLine();
        if(!StringUtils.isBlank(tempstr)&&tempstr.indexOf("\\u")>0) {
            tempstr = unicode2String(tempstr);
        }
        //汉字匹配,统计字数
        if (tempstr!=null  && !tempstr.isEmpty()){
        Matcher matcher = pattern.matcher(tempstr);
        while(matcher.find()) num_of_words++;
        }
//
//            //汉字标点匹配,统计字数
//            Matcher matcher2 = pattern2.matcher(tempstr);
//            while(matcher2.find()) num_of_wordsAndPunctuation++;
//
//            //空格匹配,统计字数
//            Matcher matcher3 = pattern3.matcher(tempstr);
//            while(matcher3.find()) num_blank++;
       // System.out.println("tempstr:/" + tempstr+"/"+tempstr.length());
        if (tempstr==null || tempstr.isEmpty()){
            linenull++;
           // System.out.println("linenull:" + linenull);
        }
        tempstr = "";
    }

    br.close(); //关闭文件
//
    return num_of_words;
}


public static String unicode2String(String unicode){  
    if(StringUtils.isBlank(unicode))return null;

    StringBuilder sb = new StringBuilder();  
    int i = -1;  
    int pos = 0;  

    while((i=unicode.indexOf("\\u", pos)) != -1){  
        sb.append(unicode.substring(pos, i));  
        if(i+5 < unicode.length()){  
            pos = i+6;  
            sb.append((char)Integer.parseInt(unicode.substring(i+2, i+6), 16));  
        }  
    }  

    return sb.toString();  
} 


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值