Is there a way to check if in BufferedReader
object is something to read? Something like C++ cin.peek()
. Thanks.
The read() method of BufferedReader class in Java is used to read a single character from the given buffered reader. This read() method reads one character at a time from the buffered stream and return it as an integer value. Overrides: It overrides the read() method of Reader class.
It will read n bytes or chars data and store into a char array instead of reading again and again from i/o. Remember every read operation, is costlier and will have impact on performance.
Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect. So, if you don't close(), system resources may be still associated with the reader which may cause memory leak.
When you are finished reading characters from the BufferedReader you should remember to close it. Closing a BufferedReader will also close the Reader instance from which the BufferedReader is reading.
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
You can try the "boolean ready()" method. From the Java 6 API doc: "A buffered character stream is ready if the buffer is not empty, or if the underlying character stream is ready."
BufferedReader r = new BufferedReader(reader); if(r.ready()) { r.read(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With