El método skip (largo) de PushbackReader Class en Java se usa para omitir el número especificado de caracteres en la secuencia. Este número de caracteres se especifica como parámetro. Si llega al final de la transmisión omitiendo, bloqueará la transmisión hasta que obtenga algunos caracteres o lanzará IOException.
Sintaxis:
public long skip(long numberOfChar)
Parámetros: este método acepta un parámetro numberOfChar que es el número de caracteres que este método omitirá.
Valor devuelto: este método devuelve un valor largo que es el número real de caracteres omitidos por este método.
Excepción: este método arroja:
- IOException: si se produce algún error durante la salida de entrada
- IllegalArgumentException: si el número de caracteres pasados es negativo.
Los siguientes métodos ilustran el funcionamiento del método skip(long):
Programa 1:
// Java program to demonstrate // PushbackReader skip(long) method import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { try { // Initializing a StringReader // and PushbackReader String s = "GeeksForGeeks"; StringReader stringReader = new StringReader(s); PushbackReader pushbackReader = new PushbackReader(stringReader); // Get the character // to be read from the stream int ch; // Read the first 5 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 < 5; i++) { ch = pushbackReader.read(); System.out.print((char)ch); } System.out.println(); // skip 3 characters using skip(long) pushbackReader.skip(3); System.out.println("Next 3 characters skipped."); // Read the 2 characters // to this reader using read() method for (int i = 0; i < 2; i++) { ch = pushbackReader.read(); System.out.print((char)ch); } System.out.println(); // Close the stream pushbackReader.close(); System.out.println("Stream Closed."); } catch (Exception e) { System.out.println(e); } } }
Geeks Next 3 characters skipped. Ge Stream Closed.
Programa 2:
// Java program to demonstrate // PushbackReader skip(long) method import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { try { // Initializing a StringReader // and PushbackReader String s = "GFG"; StringReader stringReader = new StringReader(s); PushbackReader pushbackReader = new PushbackReader(stringReader); // Get the character // to be read from the stream int ch; // Read the first character // to this reader using read() method // This will put the str in the stream // till it is read by the reader ch = pushbackReader.read(); System.out.print((char)ch); System.out.println(); // skip 1 characters using skip(long) pushbackReader.skip(1); System.out.println("Next 1 characters skipped."); // Read the next 1 character // to this reader using read() method for (int i = 0; i < 1; i++) { ch = pushbackReader.read(); System.out.print((char)ch); } System.out.println(); // Close the stream pushbackReader.close(); System.out.println("Stream Closed."); } catch (Exception e) { System.out.println(e); } } }
G Next 1 characters skipped. G Stream Closed.
Referencia: https://docs.oracle.com/javase/9/docs/api/java/io/PushbackReader.html#skip-long-