java面试题:写出下列代码的作用
publicstaticintcountWordInFile(Stringfilename,Stringword){intcounter=0;try(FileReader...
public static int countWordInFile(String filename,String word){
int counter=0;
try(FileReader fr=new FileReader(filename)){
try(BufferedReader br=new BufferedReader(fr)){
String line=null;
while((line=br.readLine())!=null) {
int index=-1;
while (line.length()>=word.length()&&(index=line.indexOf(word))>=0) {
counter++;
line=line.substring(index+word.length());
}
}
}
}catch (Exception ex) {
ex.printStackTrace();
}
return counter;
} 展开
int counter=0;
try(FileReader fr=new FileReader(filename)){
try(BufferedReader br=new BufferedReader(fr)){
String line=null;
while((line=br.readLine())!=null) {
int index=-1;
while (line.length()>=word.length()&&(index=line.indexOf(word))>=0) {
counter++;
line=line.substring(index+word.length());
}
}
}
}catch (Exception ex) {
ex.printStackTrace();
}
return counter;
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询