下面的代码从文件“helpFile.txt”中读取并检查由“#”表示的索引。从文件中读取的变量存储在整数 c 中,如果读取的字符是“#”,则与“#”进行比较,而不将整数转换为字符。我想知道比较是否有效,因为编译器没有显示任何错误。另外,假设程序在文件中找到了“#”,并且一个名为“topic”的字符串紧跟在“#”之后,并使用 readLine() 读取它。'String info = br.readLine()' 将只是 'topic' 还是 '#'+'topic'?抱歉问了这么长的问题。非常感谢帮助。
boolean helpOn(字符串什么){
private BufferedReader br;
private String info, topic;
private static PrintWriter pw = new PrintWriter(System.out, true);
int c;
br = new BufferedReader(new FileReader("helpFile.txt"));
try{
do{
//read characters until '#' is found
c = br.read();
if(***c=='#'***){ //check if the character is '#'
pw.println(c);
if((**topic=br.readLine()**).equalsIgnoreCase(what)){ //check if 'what' string is equal to 's' which is the string after '#'
while((info=br.readLine())!=null){ //print info until the end of info
if(info!=null)
pw.println(info);
}
return true;
}
}
}
while(c!=-1);
}
catch(IOException ex){
pw.println("File error.");
ex.printStackTrace();
try{
br.close();
}
catch(IOException e){
pw.println("Error closing file.");
e.printStackTrace();
}
return false;
}
try{
br.close();
}
catch(IOException ex){
pw.println("Error closing file.");
ex.printStackTrace();
}
return false; //topic not found
}