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 mark() method
Selected Reading
Java - CharArrayReader mark() methodDescriptionThe Java CharArrayReader mark(int readAheadLimit) method is used to mark the current position in the stream. Subsequent calls to reset() will return the stream to this marked position, provided the mark has not been invalidated. The readAheadLimit parameter is ignored in CharArrayReader, as it works with an in-memory character array and does not require special handling for read limits. DeclarationFollowing is the declaration for java.io.CharArrayReader.mark(int readAheadLimit) method − public void mark(int readAheadLimit) ParametersreadAheadLimit− The parameter sets the limit on the number of characters that can be read while preserving the mark. The argument is normally ignored due to the fact that there is no actual limit as the stream's input comes from character array. Return ValueThe method doesn't return any value. ExceptionIOException− If an I/O error occurs. Example - Using CharArrayReader mark() methodThe following example shows the usage of Java CharArrayReader mark() 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);
// read and print the characters from the stream
System.out.println(car.read());
System.out.println(car.read());
// mark() is invoked at this position
car.mark(0);
System.out.println("Mark() is invoked");
System.out.println(car.read());
System.out.println(car.read());
// reset() is invoked at this position
car.reset();
System.out.println("Reset() is invoked");
System.out.println(car.read());
System.out.println(car.read());
System.out.println(car.read());
} catch(IOException e) {
// if I/O error occurs
System.out.print("Stream is already closed");
} 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 − 65 66 Mark() is invoked 67 68 Reset() is invoked 67 68 69 Example - Basic Use of mark(int readAheadLimit)The following example shows the usage of Java CharArrayReader mark(readAheadLimit) method. CharArrayReaderDemo.javapackage com.tutorialspoint;
import java.io.CharArrayReader;
import java.io.IOException;
public class CharArrayReaderDemo {
public static void main(String[] args) {
char[] data = "Hello, World!".toCharArray();
// Create a CharArrayReader
CharArrayReader reader = new CharArrayReader(data);
try {
// Read and print the first three characters
System.out.print("Initial characters: ");
for (int i = 0; i < 3; i++) {
System.out.print((char) reader.read());
}
System.out.println();
// Mark the current position
reader.mark(0); // The readAheadLimit is ignored in CharArrayReader
System.out.println("Marked current position.");
// Read and print the next three characters
System.out.print("Next characters: ");
for (int i = 0; i < 3; i++) {
System.out.print((char) reader.read());
}
System.out.println();
// Reset the stream to the marked position
reader.reset();
System.out.println("Stream reset to the marked position.");
// Read and print the same three characters again
System.out.print("Characters after reset: ");
for (int i = 0; i < 3; i++) {
System.out.print((char) reader.read());
}
System.out.println();
} 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 characters: Hel Marked current position. Next characters: lo, Stream reset to the marked position. Characters after reset: lo, Explanation
Example - Skipping and Resetting with mark(int readAheadLimit)The following example shows the usage of Java CharArrayReader mark() 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".toCharArray();
// Create a CharArrayReader
CharArrayReader reader = new CharArrayReader(data);
try {
// Read and print the first four characters
System.out.print("Initial characters: ");
for (int i = 0; i < 4; i++) {
System.out.print((char) reader.read());
}
System.out.println();
// Mark the current position
reader.mark(0);
System.out.println("Marked current position.");
// Skip the next five characters
reader.skip(5);
System.out.println("Skipped 5 characters.");
// Read and print the next two characters
System.out.print("Characters after skipping: ");
for (int i = 0; i < 2; i++) {
System.out.print((char) reader.read());
}
System.out.println();
// Reset the stream to the marked position
reader.reset();
System.out.println("Stream reset to the marked position.");
// Read and print characters from the reset position
System.out.print("Characters after reset: ");
for (int i = 0; i < 4; i++) {
System.out.print((char) reader.read());
}
System.out.println();
} 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 characters: Java Marked current position. Skipped 5 characters. Characters after skipping: ra Stream reset to the marked position. Characters after reset: Pro Explanation
Key Points
java_io_chararrayreader.htm Advertisements
|
| You are visitor 01402503 — 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. |