我们可以 choose/modify readLine() 方法从哪里开始读取?

can we choose/modify where readLine() method starts reading?

我搜索了所有的主题,由于没有找到类似的问题,所以我想问以下问题:

给定示例代码:

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 if (condition) statement; else statement; "

问题是:

为什么上例中的readLine()方法没有读取'#',而是有下面的output:if(条件)语句; else 语句;

提前感谢大家的帮助!

发生这种情况是因为您已经 阅读 # 字符,因此当您阅读 read()readLine() 时不再存在。

你能做的是:

String ch = helpRdr.readLine();
if (ch.startsWith("#")) {
 // Do your stuff
}

或者这样:

topic = ch + helpRdr.readLine();