如何用java读取文件并统计文件的大小写字母,数字,特殊字符的个数
2个回答
展开全部
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
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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询