java讀取文件分析行數空格字符數字
package com.sgai;
import java.io.BufferedReader;
import java.io.FileReader;
/**
* @category 本文僅限于數字,字母,及空格
*
* @author 王俊磊
*
*/
public class test {
public static void main(String[] args) {
String fileName = "c:/temp/hello.txt",line;
int i = 0,j = 0,f = 0,k = 0;
try {
BufferedReader in = new BufferedReader(new FileReader(fileName));
line = in.readLine();
while(null != line){
for (int l = 0; l < line.length(); l++) {
if(Character.isLetter(line.charAt(l)))
i++;
else if(Character.isDigit(line.charAt(l)))
j++;
else if(Character.isSpaceChar(line.charAt(l)))
f++;
}
line = in.readLine();
k++;
}
in.close();
System.out.println("字母="+i+"數字="+j+"空格="+f+"行數="+k);
} catch (Exception e) {
// TODO: handle exception
}
}
}
|