我搜索了所有主题,由于找不到类似的问题,我想问以下问题:
给出了示例代码:
try (BufferedReader helpRdr= new BufferedReader (new FileReader (helpfile))){
do {
//read characters until # is found
ch= helpRdr.read();
//now see if topics match
if (ch=='#') {
topic= helpRdr.readLine();
if (what.compareTo(topic)==0) { //found topic
do {
info=helpRdr.readLine();
if (info!=null) System.out.println (info);
} while ((info!=null) && (info.compareTo("")!=0));
和示例文件内容:
“#if 如果(条件)声明; 其他声明; “
问题是:
为什么上面的示例中的readLine()方法不读取'#',而是显示以下输出:if(condition)语句; else语句;
感谢提前帮助的人!
答案 0 :(得分:1)
之所以会这样,是因为您已经读了#
字符,因此在执行read()
或readLine()
时不再存在。
您可以执行以下操作:
String ch = helpRdr.readLine();
if (ch.startsWith("#")) {
// Do your stuff
}
或者这个:
topic = ch + helpRdr.readLine();