java.io.BufferedReader mark(int readAheadLimit)

Description :

This java tutorial shows how to use the mark() method of BufferedReader class of java.io package. This method returns void and mark the present position in the stream. Make a note that a subsequent call to reset() will attempt to re position the stream to this point. This method overrides the mark() method of Reader class.

Method Syntax :

public void mark(int readAheadLimit) throws IOException

Parameter Input :

 

DataType Parameter Description
int readAheadLimit this parameter input is the limit on the number of characters that may be read but preserving the current mark.

 

Method Returns :

This method returns void but mark the present position on the stream.

Compatibility Version :

Requires Java 1.1 and up

Exception :

IllegalArgumentException

– this exception will be thrown if the readAheadLimit is negative number or less than 0

IOException

– the IOException will be thrown my mark() method if I/O error occurs.

Java Code Example :

This java example source code demonstrates the use of mark() method of BufferedReader class. Basically we mark the stream and use the reset method to go back to the marked index.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/*
 * Java example source code that uses the read() method of
 * BufferedReader class
 * This example java program reads a user input and then we print
 * the char that has been read
 */

public class BufferedReaderMark {

	public static void main(String[] args) {
		System.out.print("Enter Characters: ");
		// declare the BufferedReader Class
		// used the InputStreamReader to read the console input
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				System.in));

		// catch the possible IOException by the read() method
		try {

			System.out.println((char)reader.read());
			System.out.println((char)reader.read());
			System.out.println((char)reader.read());
			reader.mark(6);
			System.out.println("Printing characters after mark");
			System.out.println((char)reader.read());
			System.out.println((char)reader.read());
			System.out.println((char)reader.read());
			reader.reset();
			System.out.println("Printing characters after reset");
			System.out.println((char)reader.read());
			System.out.println((char)reader.read());
			System.out.println((char)reader.read());
			// close the BufferedReader object
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Sample Output :

Running the mark() method example source code of BufferedReader class will give you the following output

Enter Characters: 123456789
1
2
3
Printing characters after mark
4
5
6
Printing characters after reset
4
5
6

Exception Scenario :

Exception in thread "main" java.lang.IllegalArgumentException: Read-ahead limit < 0
	at java.io.BufferedReader.mark(Unknown Source)
	at com.javatutorialhq.java.examples.BufferedReaderMark.main(BufferedReaderMark.java:30)

Similar Method :

  • N/A

Suggested Reading List :

References :