import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringSearch{
public static void main(String[] args) throws IOException {
String src = "dadadabcadadabc";
String dsc="abc";
// System.out.println(regex(src, dsc));
// System.out.println(substring(src, dsc));
// System.out.println(inputStream("d:/a.txt", dsc));
}
// regex
public static int regex(String src,String dsc){
Pattern pattern = Pattern.compile(dsc);
Matcher matcher = pattern.matcher(src);
int num=0;
while(matcher.find()){
num++;
}
return num;
}
// replace
public static int substring(String src,String dsc){
int num=0;
while(src.contains(dsc)){
src=src.substring(src.indexOf(dsc)+dsc.length());
num++;
}
return num;
}
// inputStream
public static int inputStream(String filepath,String dsc) throws IOException{
InputStream is= new FileInputStream(filepath);
int num=0;
byte[] b = new byte[124];
while(is.read(b)!=-1){
// 第一种
String src = new String(b,0,b.length);
while(src.contains(dsc)){
src=src.substring(src.indexOf(dsc)+dsc.length());
num++;
}
// 第二种
// String[] sArr= new String(b,0,b.length).split("");
// for (String string : sArr) {
// if(string.equals(dsc)){
// num++;
// }
// }
}
is.close();
return num;
}
}