---
title: "查询文件中某个字符串出现的次数"
author: "cpf1985"
date: "2009-08-21T12:02:49Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2009-08-21T12:02:49Z"
id: 454320
url: "https://cpf1985.iteye.com/blog/454320"
---

```java

package corejava;
/**
 * 
 * 取文件中与给定的字符串相同的个数 不错
 */
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;

public class testFile {
public int count(String file,String str){
	int count=0;
	try {
		int c=0;
		Reader in =new FileReader(file);
		while((c=in.read())!=-1){
		
			while(c==str.charAt(0)){
				char cc=(char) c;
				System.out.println(cc);
				
				for(int i=1;i<str.length();i++){
					
					c=in.read();
					if(c!=str.charAt(i)){
						break;
					}
					if(i==str.length()-1){
						count++;
					}
				}
			}
		}
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	System.out.println(count);
	return count;
}
public static void main(String[] args) {
	new testFile().count("D://data.sql","kk");
}
}



```