java文件操作代码片断实例实现统计文件中字母出现的个数功能
发布时间:2016-12-28 来源:查字典编辑
摘要:复制代码代码如下:StringfileName="D:/date.java.bak";//StringfileName="D:/test.q...
复制代码 代码如下:
String fileName = "D:/date.java.bak";
// String fileName = "D:/test.qqq";
String line;
int i = 0, j = 0, f = 0, k = 0;
try {
BufferedReader in = new BufferedReader(new FileReader(fileName));
line = in.readLine();
while (line != null) {
// System.out.println(line);
char c[] = line.toCharArray();
for (int i1 = 0; i1 < c.length; i1++) {
// 如果是字母
if (Character.isLetter(c[i1]))
i++;
// 如果是数字
else if (Character.isDigit(c[i1]))
j++;
// 是空格
else if (Character.isWhitespace(c[i1]))
f++;
}
line = in.readLine();
k++;
}
in.close();
System.out
.println("字母:" + i + ",数字:" + j + ",空格:" + f + ",行数:" + k);
} catch (IOException e) {
e.printStackTrace();
}