Java Methods of BufferedReader
In addition to the basic read methods described earlier, the BufferedReader
class in Java provides several other useful methods for reading data from a character stream. Here are some of the methods available in the BufferedReader
class:
mark(int readAheadLimit)
: This method marks the current position in the stream, so that the stream can be reset to this position later. ThereadAheadLimit
parameter specifies the number of characters that can be read after the mark before it becomes invalid.reset()
: This method resets the stream to the last marked position. If the stream has not been marked, anIOException
is thrown.skip(long n)
: This method skips over and discards up ton
characters of data from the input stream.read(char[] cbuf)
: This method reads an array of characters from the input stream and returns the number of characters read.read(char[] cbuf, int off, int len)
: This method reads up tolen
characters from the input stream into thecbuf
character array, starting at the specifiedoff
offset. It returns the number of characters read, or -1 if the end of the stream has been reached.readLine()
: This method reads a line of text from the input stream and returns it as a string. If the end of the stream has been reached, it returnsnull
.lines()
: This method returns aStream<String>
of the lines from the input stream. This method can be useful for processing large files, as it allows you to read the file line by line without loading the entire file into memory at once.close()
: This method closes the input stream and releases any system resources associated with it.
Note that many of these methods can throw an IOException
if there is an error while reading from the stream. Therefore, it is important to handle exceptions appropriately when using these methods.