
java输入/输出流体系中常用的流的分类表
/**
* Test.txt文件内容 affaaaefssafafeava
* @param args
*/
public static void main(String[] args) {
File file = new File("D:\\Files\\Test.txt");
getNumber(file,'a');
}
public static void getNumber(File file,char ch){
//判断文件不为空
if (file.isFile() && file.length() != 0){
try {
//字符输入流
FileReader fileReader = new FileReader(file);
int a = 0;
//计数器
int count = 0;
while ((a = fileReader.read()) != -1){
if (a == ch){
count++;
}
}
System.out.println(ch+"在文件中一共出现了"+count+"次");
} catch (IOException e) {
e.printStackTrace();
}
}else {
System.out.println("文件不合法");
}
}
//输出结果a在文件中一共出现了8次
网友评论