一、实验内容
读取统计txt文件中的英文单词数目。
二、实验组织运行要求
每个同学独立完成上述实验要求。
三、程序
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class five {
public static void main(String[] args){
int count = 0;
File f = new File("C:\\Users\\hehe\\Desktop\\555.txt");
String s = "";
StringBuffer sb = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new FileReader(f));
while((s=br.readLine())!=null){
sb.append(s+'\n');
}br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Pattern p = Pattern.compile("\\b[a-zA-Z]+\\b");
Matcher m = p.matcher(sb.toString());
while(m.find()){
System.out.println(m.group());
count++;
}
System.out.println("总共"+count+"个单词");
}
}
四、实验结果