Can I peek on a BufferedReader?

后端 未结 8 1845
别跟我提以往
别跟我提以往 2021-01-01 14:02

Is there a way to check if in BufferedReader object is something to read? Something like C++ cin.peek(). Thanks.

8条回答
  •  不思量自难忘°
    2021-01-01 14:17

    You can use a PushbackReader. Using that you can read a character, then unread it. This essentially allows you to push it back.

    PushbackReader pr = new PushbackReader(reader);
    char c = (char)pr.read();
    // do something to look at c
    pr.unread((int)c); //pushes the character back into the buffer
    

提交回复
热议问题