搜索一个文件中好字的个数

public class FileReaderDemo {
//需求:搜索一个文件中好字的个数
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("e:\\templatefile\\file.txt");//这个流底层使用的是fileinputstream
int ch = 0;
int count = 0;
while((ch = fr.read())!= -1){
if(ch == '你'){
count++;
}
}
System.out.println("count="+count);
fr.close();
}
}
本文介绍了一个简单的Java程序,该程序能够读取指定文件并计算文件中特定字符出现的次数。通过使用FileReader和while循环,程序可以逐个字符地读取文件,并统计特定字符的数量。
1万+

被折叠的 条评论
为什么被折叠?



