汉字在java中占两个字符(char). 处理字符,汉字,字母不要通过inputStream来操作.最开始,对于处理数字,字母,空格还可以通过inputStream来处理,但是对于汉字就不行了.inputStream.read()返回类型为int,遇到汉字时不能一次性返回两个字符的类型,所以正则表达式总是匹配不到.
对于汉字匹配等相关字符串处理,直接通过guava的readLines()方法,将文件读为一个List<String>,再去处理每个String对象,一般会很容易操作.
while ((charCode = inputStream.read()) != -1) {
if (('a' <= charCode && charCode <= 'z') || 'A' <= charCode && charCode <= 'Z') {
if (!letterNumberMap.containsKey((char) charCode)) {
letterNumberMap.put((char) charCode, 1);
} else {
letterNumberMap.put((char) charCode, letterNumberMap.get((char) charCode) + 1);
}
letterNumber++;
} else if (('0' <= charCode) && charCode <= '9') {
if (!digitNumberMap.containsKey((char) charCode)) {
digitNumberMap.put((char) charCode, 1);
} else {
digitNumberMap.put((char) charCode, digitNumberMap.get((char) charCode) + 1);
}
digitNumber++;
} else if ((char) charCode == ' ') {
spaceNumber++;
}
}
<span style="font-size:18px;">public static void countChinese(File file) throws IOException {
List<String> strings = Files.readLines(file, Charsets.UTF_8);
String line;
Iterator<String> stringIterator = strings.iterator();
int chineseCounter = 0;
while (stringIterator.hasNext()) {
line = stringIterator.next();
chineseCounter += processLine(line);
}
logger.info("chinese number is:" + chineseCounter);
}
static int processLine(String stringLine) {
int chineseCounter = 0;
Pattern chineseCharPattern = Pattern.compile("[\\u4e00-\\u9fa5]");
Matcher chineseCharMatcher;
chineseCharMatcher = chineseCharPattern.matcher(stringLine);
while (chineseCharMatcher.find()) {
chineseCounter++;
}
return chineseCounter;
}</span>