package com.test.read;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class ReadText {
public static void main(String[] args) {
String path="C:\Users\Administrator\Desktop\11.txt";
File file=new File(path);
String str="aa";
int count=0;
try {
count = readSize(file, str);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("次数"+count);
}
public static int readSize(File file,String str) throws IOException {
BufferedReader br=new BufferedReader(new FileReader(file));
StringBuffer sbuffer=new StringBuffer();
//循环获取文本中的字符串
//逐行获取文本的字符
while(true) {
String line=br.readLine();
if(line==null) {
break;
}
sbuffer.append(line);
}
String resultStr=sbuffer.toString();
int count = 0;
int index = 0;
while (true) {
index = resultStr.indexOf(str , index + 1);
if (index > 0) {
count++;
}else {
break;
}
}
br.close();
return count;
}
}