标签:fun throws span try adl out buffere ber files
一
public static int Count(String fileName , String str) throws Exception{
//获取文件中的字符
FileReader fr = new FileReader(new File(fileName));
BufferedReader br = new BufferedReader(fr);
StringBuffer sb = new StringBuffer();
String line;
while((line = br.readLine()) != null ){
sb.append(line);
}
String filestr = sb.toString(); //文件中的字符
System.out.println(filestr);
//统计这个字符串在这个文件中出现的次数
int num = 0;
while(filestr.length() > str.length()){
int index = filestr.indexOf(str);
if(index>-1){ //存在字符串str
num++;
filestr = filestr.substring(index+str.length());
}
else{
break;
}
}
return num;
}
二:
public
static
int
fun(File file,String s){
int
count=
0
;
String read;
String readAll =
""
;
int
i=
0
;
try
{
BufferedReader br =
new
BufferedReader(
new
InputStreamReader(
new
FileInputStream(file)));
while
((read = br.readLine())!=
null
){
readAll += read;
}
while
(readAll.indexOf(s,i)!=-
1
){
i=readAll.indexOf(s,i)+s.length();
count++;
}
}
catch
(FileNotFoundException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
return
count;
}
写入一个方法,输入一个文件名和一个字符串,统计这个字符串在这个文件中出现的次数。
标签:fun throws span try adl out buffere ber files
原文地址:http://www.cnblogs.com/qiong2017/p/7678461.html