GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java - BufferedReader skip(long n) method
Selected Reading
Java - BufferedReader skip(long n) methodDescriptionThe Java BufferedReader skip(long n) method skips over a specified number of characters in the stream. It moves the reader forward by n characters, discarding the skipped characters. It returns the actual number of characters skipped, which might be less than n if the end of the stream is reached before skipping n characters. DeclarationFollowing is the declaration for java.io.BufferedReader.skip(long n) method. public long skip(long n) Parametersn − n number of characters to be skipped. Return ValueThis method returns actual number of characters skipped. Exception
Example - Using a BufferedReader skip(long n) methodThe following example shows the usage of BufferedReader skip(long n) method. BufferedReaderDemo.javapackage com.tutorialspoint;
import java.io.BufferedReader;
import java.io.StringReader;
public class BufferedReaderDemo {
public static void main(String[] args) throws Exception {
String s ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
StringReader sr = null;
BufferedReader br = null;
try {
// create and assign a new string reader
sr = new StringReader(s);
// create new buffered reader
br = new BufferedReader(sr);
// reads and prints BufferedReader
int value = 0;
while((value = br.read()) != -1) {
// skips a character
br.skip(1);
System.out.print((char)value);
}
} catch (Exception e) {
// exception occurred.
e.printStackTrace();
} finally {
// releases any system resources associated with the stream
if(sr!=null)
sr.close();
if(br!=null)
br.close();
}
}
}
OutputLet us compile and run the above program, this will produce the following result − ACEGIKMOQSUWY Example - Skipping Characters and Reading the Remaining InputThe following example shows the usage of BufferedReader skip(long n) method. BufferedReaderDemo.javapackage com.tutorialspoint;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
public class BufferedReaderDemo {
public static void main(String[] args) {
String input = "Hello, World! This is a BufferedReader skip() example.";
// Initialize BufferedReader with a StringReader
try (BufferedReader reader = new BufferedReader(new StringReader(input))) {
// Skip the first 7 characters
long skipped = reader.skip(7);
System.out.println("Skipped characters: " + skipped);
// Read and print the remaining input
String remaining = reader.readLine();
System.out.println("Remaining input: " + remaining);
} catch (IOException e) {
System.err.println("An error occurred: " + e.getMessage());
}
}
}
OutputLet us compile and run the above program, this will produce the following result − Skipped characters: 7 Remaining input: World! This is a BufferedReader skip() example. Explanation
Example - Using skip() to Move Past Unwanted CharactersThe following example shows the usage of BufferedReader skip(long n) method. BufferedReaderDemo.javapackage com.tutorialspoint;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
public class BufferedReaderDemo {
public static void main(String[] args) {
String input = "12345-ABCDE-67890";
// Initialize BufferedReader with a StringReader
try (BufferedReader reader = new BufferedReader(new StringReader(input))) {
// Read the first 5 characters
char[] firstPart = new char[5];
reader.read(firstPart, 0, 5);
System.out.println("First part: " + new String(firstPart));
// Skip the next 6 characters (the hyphen and "ABCDE")
long skipped = reader.skip(6);
System.out.println("Skipped characters: " + skipped);
// Read the remaining input
String remaining = reader.readLine();
System.out.println("Remaining part: " + remaining);
} catch (IOException e) {
System.err.println("An error occurred: " + e.getMessage());
}
}
}
OutputLet us compile and run the above program, this will produce the following result − First part: 12345 Skipped characters: 6 Remaining part: -67890 Explanation
Key Points About skip()
These examples show how the skip() method can be used to efficiently move through parts of an input stream. java_io_bufferedreader.htm Advertisements
|
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |