The CharArrayReader
class in Java is a subclass of the abstract class Reader
. It is used to read character data from a character array, which is passed to its constructor. This class allows the programmer to treat a character array as a character-stream.
The CharArrayReader
class provides several methods to read characters from the array, such as read()
, read(char[] cbuf, int off, int len)
, and skip(long n)
. The read()
method reads a single character from the array, while the read(char[] cbuf, int off, int len)
method reads a sequence of characters into a buffer. The skip(long n)
method skips over n
characters in the array.
The CharArrayReader
class also has a mark(int readAheadLimit)
method, which allows the programmer to mark a position in the array and later reset to that position using the reset()
method. The markSupported()
method can be used to check whether the mark()
method is supported by the CharArrayReader
instance.
Here is an example of using the CharArrayReader
class to read character data from a character array:
char[] chars = {'a', 'b', 'c', 'd', 'e'}; CharArrayReader reader = new CharArrayReader(chars); int c; while ((c = reader.read()) != -1) { System.out.print((char) c); }
This will output:
abcde
Java CharArrayReader class declaration:
The declaration of the CharArrayReader
class in Java is as follows:
public class CharArrayReader extends Reader
This indicates that CharArrayReader
is a public class that extends the Reader
class, which is an abstract class in the java.io
package. The Reader
class provides a framework for reading character-based data streams.
The CharArrayReader
class can be used to read character data from an array of characters. The constructor of CharArrayReader
takes a character array as its parameter, which is the source of the character data that will be read.
Here is an example of creating an instance of CharArrayReader
:
char[] chars = {'a', 'b', 'c', 'd', 'e'}; CharArrayReader reader = new CharArrayReader(chars);
This creates a new CharArrayReader
instance that reads character data from the chars
array. The reader
instance can then be used to read character data from the array using the methods provided by the Reader
class.
Java CharArrayReader class methods:
The CharArrayReader
class in Java provides several methods for reading character data from a character array. Here are some of the most commonly used methods:
read()
: This method reads a single character from the character array and returns it as an integer. If the end of the character array is reached, it returns -1.
int read() throws IOException
2.read(char[] cbuf, int off, int len)
: This method reads up to len
characters from the character array into the provided character buffer cbuf
, starting at the given offset off
. It returns the number of characters read, or -1 if the end of the character array has been reached.
int read(char[] cbuf, int off, int len) throws IOException
3.skip(long n)
: This method skips over n
characters in the character array. It returns the actual number of characters skipped.
long skip(long n) throws IOException
4.mark(int readAheadLimit)
: This method marks the current position in the character array. The readAheadLimit
parameter specifies the maximum number of characters that can be read while still allowing the reset()
method to be called to reset to the marked position.
void mark(int readAheadLimit)
5.reset()
: This method resets the CharArrayReader
to the last marked position, or to the beginning of the character array if mark()
has not been called.
void reset() throws IOException
6.markSupported()
: This method returns true
if the mark()
and reset()
methods are supported by the CharArrayReader
instance.
boolean markSupported()
These methods provide the basic functionality for reading character data from a character array using the CharArrayReader
class in Java.
Example of CharArrayReader Class:
Here is an example of using the CharArrayReader
class in Java to read character data from a character array:
import java.io.CharArrayReader; import java.io.IOException; public class CharArrayReaderExample { public static void main(String[] args) { char[] chars = {'a', 'b', 'c', 'd', 'e'}; CharArrayReader reader = new CharArrayReader(chars); try { int c; while ((c = reader.read()) != -1) { System.out.print((char) c); } // Reset the reader and read into a buffer reader.reset(); char[] buffer = new char[3]; int numRead; while ((numRead = reader.read(buffer, 0, buffer.length)) != -1) { System.out.print(new String(buffer, 0, numRead)); } } catch (IOException e) { e.printStackTrace(); } finally { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } }
This will output:
abcde abc de
In this example, a CharArrayReader
instance is created with a character array containing the letters ‘a’ to ‘e’. The read()
method is called repeatedly to read each character from the array and output it to the console.
After the first loop, the reader is reset using the reset()
method. Then, the read(char[] cbuf, int off, int len)
method is used to read the remaining characters from the array into a buffer of size 3. The new String()
constructor is used to create a new string from the buffer, and the number of characters read is specified to avoid outputting any unused space in the buffer.
Finally, the close()
method is called to release any resources associated with the CharArrayReader
.