我正在尝试在 java 中编写一个方法,我从文件中获取一些信息并查看该文件是否包含用户查找的信息。但是,对于我提供的代码,eclipse 表明我在“return true;”行中存在资源泄漏;并且“br = new BufferedReader(fr);” 永远不会关闭,尽管我在程序末尾使用了 close() 方法。显然我错过了一些东西。有人可以帮我弄清楚发生了什么吗?非常感谢提前!
import java.io.*;
class Help{
String helpfile;
Help(String fname){
helpfile = fname;
}
boolean helpon(String what){
FileReader fr;
BufferedReader br;
int ch;
String topic, info;
try{
fr = new FileReader(helpfile);
br = new BufferedReader(fr);
}
catch(FileNotFoundException e){
System.out.println(e);
return false;
}
try{
do{
ch = br.read();
if(ch=='#'){
topic = br.readLine();
if(what.compareTo(topic) == 0){
do{
info = br.readLine();
if(info!=null)
System.out.println(info);
}while((info!= null) && (info.compareTo("")!= 0));
return true;
}
}
}while(ch!=-1);
}
catch(IOException e){
System.out.println(e);
return false;
}
try{
br.close();
}
catch(IOException e){
System.out.println(e);
}
return false;
}
}