62,634
社区成员




import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
public class Count {
public static void main(String[] args) {
Count count = new Count();
String str = count.readFile("D://read.txt");//读
count.writeFile("D://write.txt",str);//写
}
private void writeFile(String filePath,String str) {
FileWriter fw;
try {
fw = new FileWriter(filePath);
fw.write(str);
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String readFile(String fileName){
int zm = 0;
int sz = 0;
int other = 0;
try {
@SuppressWarnings("resource")
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<s.length();i++){
char c = s.charAt(i);
if(Character.isUpperCase(c) || Character.isLowerCase(c)){
zm++;
}else if(Character.isDigit(c)){
sz++;
} else {
other++;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return "字母:"+zm+" "+"\n数字:"+sz+" "+"\n其他字符:"+other;
}
}