Método StringReader reset() en Java con ejemplos

El método reset() de la clase StringReader en Java se usa para restablecer la transmisión. Después del reinicio, si la corriente ha sido marcada, este método intenta reposicionarla en la marca, de lo contrario, intentará colocarla en el inicio.

Sintaxis:

public void reset()

Parámetros: Este método no acepta ningún parámetro.

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

Excepción: este método arroja IOException:

  • si se produce algún error durante la salida de entrada
  • si el flujo no ha sido marcado
  • si la marca ha sido invalidada
  • si el método reset() no es compatible.

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

Programa 1:

// Java program to demonstrate
// StringReader reset() 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 reset()
            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 reset() 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();
  
            // 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
Geeks

Referencia: https://docs.oracle.com/javase/9/docs/api/java/io/StringReader.html#reset–

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 *