Java Methods of BufferedReader

www.i‮aeditfig‬.com

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:

  1. mark(int readAheadLimit): This method marks the current position in the stream, so that the stream can be reset to this position later. The readAheadLimit parameter specifies the number of characters that can be read after the mark before it becomes invalid.

  2. reset(): This method resets the stream to the last marked position. If the stream has not been marked, an IOException is thrown.

  3. skip(long n): This method skips over and discards up to n characters of data from the input stream.

  4. read(char[] cbuf): This method reads an array of characters from the input stream and returns the number of characters read.

  5. read(char[] cbuf, int off, int len): This method reads up to len characters from the input stream into the cbuf character array, starting at the specified off offset. It returns the number of characters read, or -1 if the end of the stream has been reached.

  6. 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 returns null.

  7. lines(): This method returns a Stream<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.

  8. 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.