- package com.filecount;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- public class FileCount {
- public static int fileCount(String filename, String find) {
- int count = 0;
- int c;
- try {
- FileReader fr = new FileReader(filename);
- while ((c = fr.read()) != -1) {
- while (c == find.charAt(0)) {
- for (int i = 1; i < find.length(); i++) {//从第一个开始,因为第0个检查过了
- c = fr.read();
- if (c != find.charAt(1))
- break;
- if (i == find.length() - 1)
- count++;
- }
- }
- }
- fr.close();
- } catch (FileNotFoundException e) {
- System.out.println("没有发现这个文件");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return count;
- }
- public static void main(String[] args) {
- System.out.println(fileCount("e:/tt.txt", "北京"));
- }
- }