public int getNum(String fileName,String str){
int count = 0;
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
File file = new File(fileName);
if(!file.exists())
return 0;
fis = new FileInputStream(file);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
String s = "";
while((s=br.readLine())!=null){
for(int i=0;i<s.length();){
int n = s.substring(i).indexOf(str);
if(n!=-1)
{
count++;
i = i+n+1;
}else
break;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
br.close();
isr.close();
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return count;
}
笔试题---给定一个字符串,在一个文本文件中找出包含该字符串的个数
最新推荐文章于 2023-03-14 10:49:46 发布