Java - CharArrayReader read() method
Description
The Java CharArrayReader read() method reads a single character. If the stream ends, it returns -1.
Declaration
Following is the declaration for java.io.CharArrayReader.read() method −
public int read()
Parameters
NA
Return Value
The method returns the character read, as an integer from the range 0 to 65535. If the stream has reached it's end the read() returns -1.
Exception
IOException − If any I/O error occurs.
Example - Using CharArrayReader read() method
The following example shows the usage of Java CharArrayReader read() method.
CharArrayReaderDemo.java
package com.tutorialspoint;
import java.io.CharArrayReader;
import java.io.IOException;
public class CharArrayReaderDemo {
public static void main(String[] args) {
CharArrayReader car = null;
char[] ch = {'H', 'E', 'L', 'L', 'O'};
try {
// create new character array reader
car = new CharArrayReader(ch);
int value = 0;
// read till the end of the file
while((value = car.read())!=-1) {
char c = (char)value;
// print the character
System.out.print(c+" : ");
// print the integer
System.out.println(value);
}
} catch(IOException e) {
// if I/O error occurs
e.printStackTrace();
} finally {
// releases any system resources associated with the stream
if(car!=null)
car.close();
}
}
}
Output
Let us compile and run the above program, this will produce the following result −
H : 72 E : 69 L : 76 L : 76 O : 79
Example - Reading All Characters One by One
The following example shows the usage of Java CharArrayReader read() method.
CharArrayReaderDemo.java
package com.tutorialspoint;
import java.io.CharArrayReader;
import java.io.IOException;
public class CharArrayReaderDemo {
public static void main(String[] args) {
char[] data = "Hello, World!".toCharArray();
try (CharArrayReader reader = new CharArrayReader(data)) {
int charData;
System.out.print("Reading characters one by one: ");
while ((charData = reader.read()) != -1) { // Read one character at a time
System.out.print((char) charData); // Convert the int to char and print
}
} catch (IOException e) {
System.err.println("IOException occurred: " + e.getMessage());
}
}
}
Output
Let us compile and run the above program, this will produce the following result −
Reading characters one by one: Hello, World!
Explanation
A character array is created from the string "Hello, World!".
A CharArrayReader is initialized with this character array.
The read() method is used to read one character at a time until it returns -1 (end of stream).
Each character is printed by casting the returned integer value to a char.
Example - Counting the Number of Spaces in a Character Stream
The following example shows the usage of Java CharArrayReader read() method.
CharArrayReaderDemo.java
package com.tutorialspoint;
import java.io.CharArrayReader;
import java.io.IOException;
public class CharArrayReaderDemo {
public static void main(String[] args) {
char[] data = "Java programming is fun!".toCharArray();
try (CharArrayReader reader = new CharArrayReader(data)) {
int charData;
int spaceCount = 0;
// Read one character at a time
while ((charData = reader.read()) != -1) {
if ((char) charData == ' ') { // Check if the character is a space
spaceCount++;
}
}
System.out.println("Number of spaces in the string: " + spaceCount);
} catch (IOException e) {
System.err.println("IOException occurred: " + e.getMessage());
}
}
}
Output
Let us compile and run the above program, this will produce the following result −
Number of spaces in the string: 3
Explanation
The string "Java programming is fun!" is converted into a character array and passed to the CharArrayReader.
The read() method reads one character at a time.
Each character is checked to see if it is a space (' ').
The count of spaces is maintained in the variable spaceCount.
The total number of spaces is printed after the loop.
Key Points
Use of read()−
The read() method reads one character at a time and returns it as an integer.
When the end of the stream is reached, it returns -1.
Casting−
The integer returned by read() represents the character's Unicode value and must be cast to char to work with characters.
Efficiency−
Reading characters one by one is less efficient compared to buffered reads, but it is useful for simple tasks like processing or analyzing individual characters.