先列出定义的方法:
1. public int getWordNum(String fileName)
统计一个含有英文单词的文本文件的单词个数。
2. public void getWordNumFile(String formfileName, String word)
统计指定的文件中含有指定单词的个数。
假设指定单词是 “You”
在程序开发中,经常需要对文本文件中的内容进行读取,如果想从文件中直接读取字符便可以使用字符输入流FileReader。
通过此流可以从关联的文件中读取一个或一组字符。
接下来在 F 盘根目录下新建文件“‘countwords.txt’”并在其中输入以下字符串:(末尾是鼠标定位符)
字符流同样提供了带缓冲区的包装流,其中BufferedReader 用于对字符输入流进行包装,
其中有一个重要的方法readLine(),该方法用于一次性读取一行文本。
注意,例子中定义文件路径时使用里\\。这是因为在Windows中的目录符号为反斜线\,但反斜线\ 在Java中是特殊字符,表示转义符,所以在使用反斜线\ 时,前边应该再添加一个反斜线,即\\。除此之外目录符号也可以用正斜线/来表示。
public class TextProcessor {
public static void main(String[] args) throws IOException {
String fileName = "F:\\countwords.txt";//文件路径
String formfileName = "F:\\countwords.txt";
TextProcessor.getWordNum(fileName); //调用第一个方法
String word = "You"; // 指定单词
TextProcessor.getWordNumFile(formfileName, word); //调用第二个方法
}
// 统计一个含有英文单词的文本文件的单词个数
public static int getWordNum(String fileName) throws IOException {
File file = new File(fileName);
int count = 0; // 计数
if (file.exists()) {
FileReader fr = new FileReader(fileName); // 读取文件中的字符
//创建一个BufferedReader缓存对象
BufferedReader reader = new BufferedReader(fr);
String line = null;
StringBuffer sbf = new StringBuffer();
while ((line = reader.readLine()) != null) { // 每次读取一行文本,判断是否到文档末尾
sbf.append(line); //读取的追加到StringBuffer缓冲区里
String newString = sbf.toString(); //将sbf里边的内容定义到变量newString里
//在整个字符串中,先讲可能的标点符号换成空格
newString = newString.replace('\'', ' ');//将 单引号 号用空格替换
newString = newString.replace(',', ' ');//将 逗号 用空格替换
newString = newString.replace('.', ' ');//将 句号 用空格替换
//因为上面操作可能会造成一个单词后有多个空格,所以接下来要将字符串按空格(一个或多个)进行划分
String[] strword = newString.split("\\s+"); // 先分割 \\s+ 代表一个或多个空格
System.out.println(Arrays.toString(strword));//输出数组
for (int i = 0; i < strword.length; i++) {
/*
Pattern类用于创建一个正则表达式,也可以说创建一个匹配模式
它的构造方法是私有的,不可以直接创建,
但可以通过Pattern.complie(String regex)简单工厂方法创建一个正则表达式
find()对字符串进行匹配
*/
Pattern pattern = Pattern.compile("[a-zA-Z]*");//匹配一个字母后面接着0或者多个字母(大写或者小写)
Matcher matcher = pattern.matcher(strword[i]); // 将数组里遍历到的每一个字符串与正则表达式对比。
if(matcher.find()){ //找到了就计数
count++;
}
}
System.out.println(fileName + " 文件中有" + count + "个英文单词");
}
reader.close(); // 关闭流
fr.close();
}
return count;
}
// 统计指定的文件中含有指定单词的个数
public static void getWordNumFile(String formfileName, String word) throws IOException {
File file = new File(formfileName); // 创建文件对象
int count = 0; // 计数
if (file.exists()) {
FileReader fr = new FileReader(formfileName); // 读取文件中的字符
BufferedReader reader = new BufferedReader(fr);
String line = null;
StringBuffer sbf = new StringBuffer();
while ((line = reader.readLine()) != null) { // 每次读取一行文本,判断是否到文档末尾
sbf.append(line);
String newString = sbf.toString();
newString = newString.replace('\'', ' ');//将'号用空格替换
newString = newString.replace(',', ' ');//将逗号用空格替换
newString = newString.replace('.', ' ');//将句号用空格替换
String[] strword = newString.split("\\s+"); // 先分割 \\s+ 代表一个或多个空格
for (int i = 0; i < strword.length; i++) { // 判断里边是否含有指定单词
if (strword[i].contains(word)) {
count++;
}
}
}
System.out.println( " 文件中的" + word + "单词有" + count + "个英文单词");
reader.close();
fr.close();
}
}
}
运行结果图:
[You, can, be, as, mad, as, a, mad, dog, at, the, way, things, went, you, could, swear, curse, the, fates, But, when, it, comes, to, the, end, you, have, to, let, go]
F:\countwords.txt 文件中有32个英文单词
文件中的You单词有1个英文单词