Método de marca StringReader (int) en Java con ejemplos

El método mark() de la clase StringReader en Java se usa para marcar el flujo como el punto de control desde donde comenzará la lectura del flujo, una vez que se llame a reset(). Este método no es compatible con todas las subclases de la clase StringReader.

Sintaxis:

public void mark(int readAheadLimit)

Parámetros: este método acepta un parámetro obligatorio readAheadLimit que es el límite en la cantidad de caracteres que se pueden leer mientras se conserva la marca. Después de leer tantos caracteres, es posible que falle el intento de restablecer la transmisión.

Valor devuelto: este método no devuelve ningún valor.

Excepción: este método lanza IOException si ocurre algún error mientras no se admite la salida de entrada o el método mark().

Los siguientes métodos ilustran el funcionamiento del método mark():

Programa 1:

// Java program to demonstrate
// StringReader mark() method
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
            String str = "GeeksForGeeks";
  
            // Create a StringReader instance
            StringReader reader
                = new StringReader(str);
  
            // Get the character
            // to be read from the stream
            int ch;
  
            // Read the first 10 characters
            // to this reader using read() method
            // This will put the str in the stream
            // till it is read by the reader
            for (int i = 0; i < 10; i++) {
                ch = reader.read();
                System.out.print((char)ch);
            }
  
            System.out.println();
  
            // mark the stream for
            // 5 characters using mark()
            reader.mark(5);
  
            // reset the stream position
            reader.reset();
  
            // Read the 5 characters from marked position
            // to this reader using read() method
            for (int i = 0; i < 5; i++) {
                ch = reader.read();
                System.out.print((char)ch);
            }
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
Producción:

GeeksForGe
eks??

Programa 2:

// Java program to demonstrate
// StringReader mark() method
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
            String str = "GeeksForGeeks";
  
            // Create a StringReader instance
            StringReader reader
                = new StringReader(str);
  
            // Get the character
            // to be read from the stream
            int ch;
  
            // Read the first 10 characters
            // to this reader using read() method
            // This will put the str in the stream
            // till it is read by the reader
            for (int i = 0; i < 10; i++) {
                ch = reader.read();
                System.out.print((char)ch);
            }
  
            System.out.println();
  
            // mark the stream for
            // 10 characters using mark()
            reader.mark(10);
  
            // reset the stream position
            reader.reset();
  
            // Read the 1 characters from marked position
            // to this reader using read() method
            for (int i = 0; i < 1; i++) {
                ch = reader.read();
                System.out.print((char)ch);
            }
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
Producción:

GeeksForGe
e

Referencia: https://docs.oracle.com/javase/9/docs/api/java/io/StringReader.html#mark-int-

Publicación traducida automáticamente

Artículo escrito por Code_r y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *