答案:package Main;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Main {
public static void main(String[] args) {
int count = 0;
File file = new File("D:\\1.txt");
String s = "";
StringBuffer buffer = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
try {
while ((s = br.readLine()) != null) {
buffer.append(s + '\n');
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Pattern p = Pattern.compile("\\b[a-zA-Z]+\\b");
Matcher m = p.matcher(buffer.toString());
while(m.find()) {
System.out.println(m.group());
count ++ ;
}
System.out.println("总共:" + count + "个单词");
}
}
看看是不是你要的效果
上一个:为什么安装不到Tomcat的呢 那里显示:No java Virtual Machine found in folder: E:\java语言?为什么呢?
下一个:java开发,3、4年,我的薪资怎么样