java源程序:统计文本中的小写字母,大写字母,数字和其他字符的个数。

2024-12-28 02:16:56
推荐回答(1个)
回答1:

import java.io.FileInputStream;

public class Panduan {

public static void main(String[] args) {
Panduan panduan = new Panduan();
panduan.readFile("D://test.txt");
}

public void readFile(String fileName){
int upperChar = 0;
int lowerChar = 0;
int digit = 0;
int otherChar = 0;
try {
FileInputStream fis = new FileInputStream(fileName);
byte[] buff = new byte[1024];
while(fis.available()>0){
int len = fis.read(buff);
String s = new String(buff,0,len);
for(int i=0;i char c = s.charAt(i);
if(Character.isUpperCase(c)){
upperChar++;
} else if(Character.isLowerCase(c)){
lowerChar++;
} else if(Character.isDigit(c)){
digit++;
} else {
otherChar++;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("大写字母:"+upperChar+" "+"小写字母"+lowerChar+" "+"数字:"+digit+" "+"其他字符:"+otherChar);
}

}