Java PushBackInputStream

时间:2014-11-13 04:24:31

标签: java

为什么打印“.eq。”对于单=符号,它应该实际打印在==符号。在那里看评论。 f.read()与此有什么关系吗?

import java.io.*;
public class FPushBackInputStreamDemo {
    public static void main(String[] args) throws IOException {
        String s = "if (a == 4) a = 0;\n";
        byte b[] = s.getBytes();
        ByteArrayInputStream b1 = new ByteArrayInputStream(b);
        PushbackInputStream p = new PushbackInputStream(b1);
        int c;

        while((c=p.read()) != -1)
        {
            switch(c)
        {
            case '=':
                if((c = p.read()) == '=') //here it is checking for single '=' sign then why its printing ".eq." for both '==' signs?
                {
                    System.out.print(".eq.");
                }
                else
                {
                    System.out.print("<-");
                    p.unread(c);

                }
                break;
                default:
                    System.out.print((char) c);
                    break;
        }
    }
}

}

输出: enter image description here

1 个答案:

答案 0 :(得分:1)

打印.eq.表示双等号。

if声明的时候,你在case内已经读过前面的另一个=

while((c=p.read()) != -1)        // read one character
        {
            switch(c)            // see what it is
        {
            case '=':            //    that was a "="
                if((c = p.read()) == '=')       // read ANOTHER character  

请注意,在else部分中,您必须推回第二个字符,因为它尚未实际处理过(您刚刚确认它不是第二个=)。

  p.unread(c);