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 Class
Selected Reading
Java - CharArrayReader ClassIntroductionThe Java CharArrayReader class implements a character buffer that can be used as a character-input stream. Class declarationFollowing is the declaration for Java.io.CharArrayReader class − public class CharArrayReader extends Reader FieldFollowing are the fields for Java.io.CharArrayReader class −
Class constructors
Class methods
Methods inheritedThis class inherits methods from the following classes −
Example - Closing the Stream After UseThe following example shows the usage of Java CharArrayReader close() 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 characters
System.out.println("Reading characters:");
int charData;
while ((charData = reader.read()) != -1) {
System.out.print((char) charData);
}
// Close the reader
reader.close();
System.out.println("\nCharArrayReader closed.");
} catch (IOException e) {
System.err.println("An IOException occurred: " + e.getMessage());
}
}
}
OutputLet us compile and run the above program, this will produce the following result − Reading characters: Hello, World! CharArrayReader closed. Explanation
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 - Simple Check for markSupported()The following example shows the usage of Java CharArrayReader mark(readAheadLimit) method. CharArrayReaderDemo.javapackage com.tutorialspoint;
import java.io.CharArrayReader;
public class CharArrayReaderDemo {
public static void main(String[] args) {
char[] data = "Hello, World!".toCharArray();
// Create a CharArrayReader
CharArrayReader reader = new CharArrayReader(data);
// Check if mark is supported
if (reader.markSupported()) {
System.out.println("Mark and reset are supported by CharArrayReader.");
} else {
System.out.println("Mark and reset are NOT supported by CharArrayReader.");
}
}
}
OutputLet us compile and run the above program, this will produce the following result − Mark and reset are supported by CharArrayReader. Explanation
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. |