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 - CharArrayReader skip(long n) method
Selected Reading
Java - CharArrayReader skip(long n) methodDescriptionThe Java CharArrayReader skip(long n) method skips the specified number of characters from the current reading position. If n is greater than the remaining characters, it skips only up to the end of the stream. DeclarationFollowing is the declaration for java.io.CharArrayReader.skip(long n) method − public long skip(long n) Parametersn− The number of characters to be skipped. Return ValueThe number of characters actually skipped. ExceptionIOException − If any I/O error occurs or the stream is closed. Example - Using CharArrayReader skip() methodThe following example shows the usage of Java CharArrayReader skip() method. CharArrayReaderDemo.javapackage com.tutorialspoint;
import java.io.CharArrayReader;
import java.io.IOException;
public class CharArrayReaderDemo {
public static void main(String[] args) {
CharArrayReader car = null;
char[] ch = {'A', 'B', 'C', 'D', 'E'};
try {
// create new character array reader
car = new CharArrayReader(ch);
int value = 0;
// read till the end of the stream
while((value = car.read())!=-1) {
// convert integer to char
char c = (char)value;
// print characters
System.out.print(c + "; ");
// skip single character
long l = car.skip(1);
System.out.println("Characters Skipped : "+l);
}
} catch(IOException e) {
// if I/O error occurs
e.printStackTrace();
} finally {
// releases any system resources associated with the stream
if(car!=null)
car.close();
}
}
}
OutputLet us compile and run the above program, this will produce the following result − A; Characters Skipped : 1 C; Characters Skipped : 1 E; Characters Skipped : 0 Example - Skipping the First Few CharactersThe following example shows the usage of Java CharArrayReader skip() method. CharArrayReaderDemo.javapackage com.tutorialspoint;
import java.io.CharArrayReader;
import java.io.IOException;
public class CharArrayReaderDemo {
public static void main(String[] args) {
char[] data = "Java Programming Language".toCharArray();
try (CharArrayReader reader = new CharArrayReader(data)) {
System.out.println("Initial content: " + new String(data));
// Skip first 5 characters ("Java ")
reader.skip(5);
System.out.println("After skipping 5 characters, reading the rest:");
// Read and print remaining characters
int charData;
while ((charData = reader.read()) != -1) {
System.out.print((char) charData);
}
} catch (IOException e) {
System.err.println("IOException occurred: " + e.getMessage());
}
}
}
OutputLet us compile and run the above program, this will produce the following result − Initial content: Java Programming Language After skipping 5 characters, reading the rest: Programming Language Explanation
Example - Skipping More Characters Than AvailableThe following example shows the usage of Java CharArrayReader reset() method. CharArrayReaderDemo.javapackage com.tutorialspoint;
import java.io.CharArrayReader;
import java.io.IOException;
public class CharArrayReaderDemo {
public static void main(String[] args) {
char[] data = "Short Text".toCharArray();
try (CharArrayReader reader = new CharArrayReader(data)) {
System.out.println("Initial content: " + new String(data));
// Try to skip more characters than available
long skipped = reader.skip(50);
System.out.println("Characters skipped: " + skipped);
// Check if we reached the end
if (reader.read() == -1) {
System.out.println("Reached the end of the stream after skipping.");
}
} catch (IOException e) {
System.err.println("IOException occurred: " + e.getMessage());
}
}
}
OutputLet us compile and run the above program, this will produce the following result − Initial content: Short Text Characters skipped: 10 Reached the end of the stream after skipping. Explanation
Key Takeaways About skip(long n)
These examples show how skip() helps ignore parts of a character stream efficiently! java_io_chararrayreader.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. |