import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("c:/test.txt"));
StringBuilder sb = new StringBuilder();
while (true) {
String line = br.readLine();
if (line == null)
break;
sb.append(line);
}
br.close();
String content = sb.toString();
System.out.println("Character count is:" + (content.length() - content.replaceAll("[a-zA-Z]", "").length()));
System.out.println("Number count is:" + (content.length() - content.replaceAll("[0-9]", "").length()));
System.out.println("Special count is :" + content.replaceAll("[\\w ]", "").length());
}
}
----------------------------------
c盘下新建个test.txt文件,内容:
abc123!@#
----------------------------------
输出:
Character count is:3
Number count is:3
Special count is :3
取消
评论